reset password
Author Message
dliang
Posts: 35
Posted 16:50 Mar 17, 2019 |

I try to use the switch statement in question 2, everything works fine, but whenever I enter a value 10,11,12..... the computer counts these value as hex digits.

tnguye116
Posts: 3
Posted 17:15 Mar 17, 2019 |

Are you using .next().charAt(0) for when you read the user input?

If you are, I think the computer would only read the first character in what the user inputs, so in the case of 10, 11, and etc., it's reading it as 1, which is a hex digit. 

dliang
Posts: 35
Posted 17:35 Mar 17, 2019 |

So, you mean I should use String rather than next.charAt(0)?

dliang
Posts: 35
Posted 17:36 Mar 17, 2019 |
tnguye116 wrote:

Are you using .next().charAt(0) for when you read the user input?

If you are, I think the computer would only read the first character in what the user inputs, so in the case of 10, 11, and etc., it's reading it as 1, which is a hex digit. 

So, you mean I should use String rather than next.charAt(0)?

tnguye116
Posts: 3
Posted 18:38 Mar 17, 2019 |
dliang wrote:
tnguye116 wrote:

Are you using .next().charAt(0) for when you read the user input?

If you are, I think the computer would only read the first character in what the user inputs, so in the case of 10, 11, and etc., it's reading it as 1, which is a hex digit. 

So, you mean I should use String rather than next.charAt(0)?

I think it would be fine if you still stick with the next.charAt(0) because the requirements from the original nibble monster said to read the first character of the input and ignore the rest.

wcwir
Posts: 75
Posted 20:05 Mar 17, 2019 |

Tyler is right -- the monster only looks at the first character, so while the user enters '10', the monster only considers '1', which it gladly eats since it is a nibble.

So your code works just fine for that case

dliang
Posts: 35
Posted 02:50 Mar 18, 2019 |
wcwir wrote:

Tyler is right -- the monster only looks at the first character, so while the user enters '10', the monster only considers '1', which it gladly eats since it is a nibble.

So your code works just fine for that case

Ms.Cwir, I would like to know how to fix my NibbleMonster1 issue. Logically, the program seem work, but my output result, "food" and "Y/N" statement keep repeating twice.

 

Last edited by dliang at 16:10 Mar 18, 2019.
wcwir
Posts: 75
Posted 03:32 Mar 18, 2019 |

Hey Sam:

What happens is that when you first use your Scanner object to read user input on line 25, and the user types something and presses "Enter", your Scanner consumes the token entered by the user, and keeps waiting on the same line where that token was entered (since on line 25 you are using the method next()).

And then the next time you use your Scanner object on line 84, since you use the nextLine() method, your Scanner consumes an empty String (or whatever is after the first token entered by the user), puts into the ans variable and advances to the console line.

Meanwhile, your program compares ans to "Y", and then to "N" and since it doesn't match either, it goes back up to line 30 to evaluate the loop condition.

And since the content of your w variable has not changed, it runs again!

In the textbook, the other Liang (the one who wrote it :) talks about the perils of mixing next() and nextLine()in the same program. He says one shouldn't do it; I say you can do it, but you must use caution!