wood burning stoves*
The moose likes Beginning Java and the fly likes While Loop Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While Loop Error" Watch "While Loop Error" New topic
Author

While Loop Error

Peter Kolozvary
Greenhorn

Joined: Sep 06, 2008
Posts: 6
I'm trying to create a Dice Game for class and am getting an error when copying the way to enter the while loops straight from the book.

Here is the code and the error:

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Where in the code do you initialize the 'input' variable??
[ September 07, 2008: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

And more importantly, where do you declare it?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peter Kolozvary
Greenhorn

Joined: Sep 06, 2008
Posts: 6
Thanks for the help. I changed the input to playAgain instead but I'm now getting another error.

Okay, I'm obviously a beginner. Here is my way of thinking. Maybe you guys and gals can tell me where I'm off. I get most of the code up till this part and am confused with the repeat line below. Why would the error say string is out of range?

// Ask the user if they want to play again.
System.out.println("Would you like to play again?");
System.out.println("Y = yes, N = no ");
playAgain = keyboard.nextLine(); // Read a line.
repeat = playAgain.charAt(0); // Get the first char.

} while (repeat == 'Y' || repeat == 'y');

The system.out seems simple. It prints the message to the user and asks if they want to play again and gives them options.

Next the line starting with playAgain takes what the user types and assigns it to the playAgain string. Does that sound correct?

The line with the repeat is where I'm confused. It sounds like the repeat is assigning the first character of playAgain as what is used to determine if we're going to play again or not.

The last while statement just means to repeat if the user enters a Y or y.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You need to have a Scanner for each input - or change your code to have one
as the input 'controller', and then call for each input.
[ September 07, 2008: Message edited by: Rene Larsen ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
I think you could have got away with changing the line with input in to

playAgain = keyboard.nextLine();

Look through the String class and you find methods allowing you to test whether a String starts with something, and to change a case to lower case or upper case. That might make your while testing easier.
I would suggest for a simple application like that you will find Scanners much easier to use than BufferedReaders.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Originally posted by Campbell Ritchie:
I think you could have got away with changing the line with input in to

playAgain = keyboard.nextLine();

Look through the String class and you find methods allowing you to test whether a String starts with something, and to change a case to lower case or upper case. That might make your while testing easier.
I would suggest for a simple application like that you will find Scanners much easier to use than BufferedReaders.


The problem is that when the Scanner is used in then the call to will not be made (the run of the program don't stop at the line, for the user to type to the input stream) - therefore my suggestion of the use of one Scanner for each input - OR the use of one BufferedReader.
[ September 07, 2008: Message edited by: Rene Larsen ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Yes, I see what you mean, that Scanner sticks to the line until nextLine is called. That can be confusing, I agree. Do you think keyboard.next() would work?

Or putting the input in the while like this?

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You only get one shot with a Scanner, and when you put nextLine() inside the while loop - then you need to type ALL input in that loop in one line ("Please select a number between 2 & 12. ", "Would you like to play again?", etc.)

I don't think that this solution is good enough - it will work, but the input will be confusing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
You only put the bit about whether you want to go back in the () after while . . .

Of course, you might need a nextLine() call immediately before that.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You still need to type ALL 'answers' in only one input.

In the Javadoc for the Scanner class, are the following written:
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
I see what you mean. But a line end counts as whitespace and we usually use the enter key after input.

This works all right:You can enter 1 2 yes or

1
2
yes
but 1 2 yes prints "please enter yes" after that, then "enter two ints."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
And, by the way [pedantic mode]that's not called a while loop, it's a do loop (or a do-while loop).[/pedantic mode]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: While Loop Error
 
Similar Threads
Exception in thread "main" java.lang.NoClassDefFoundError: dice (wrong name: Dice)
Having problem compiling this answer
I keep having a issue with variables and methods.
Trouble with Guessing Game
Dice Rolling Simulation