Question About Using Scanner Object Twice
Just a general question about Scanner()...
Is it poor coding practice to use the same Scanner object in 1 method to get input for a string and then an int?
I keep running into trouble when I try to do so. I end up creating 2 scanner objects in the same method to work around the issue. I'm assuming that is probably bad practice as well.
I've even tried to insert a line like to soak up any left over information before using Scanner the second time but it never seems to work quite right. Am I missing something?
Re: Question About Using Scanner Object Twice
Quote:
Originally Posted by
derekeverett
Is it poor coding practice to use the same Scanner object in 1 method to get input for a string and then an int?
No, if the Scanner is scanning a stream or string that has different types of data in it, and you want to extract the different data types, it usually makes sense to use the same scanner to read the different data types.
Quote:
I keep running into trouble when I try to do so. I end up creating 2 scanner objects in the same method to work around the issue. I'm assuming that is probably bad practice as well.
I've even tried to insert a line like
to soak up any left over information before using Scanner the second time but it never seems to work quite right. Am I missing something?
You probably don't need to use two scanners, so using two scanners is probably not good practice. Without an explanation of what you're trying to achieve, and/or the code you tried, I can't be more specific.
If you want help with some code, post up the code and the full text of any error messages and stack traces, explain what you're trying to do, and explain what is going wrong.
Re: Question About Using Scanner Object Twice
Here's a small piece as an example.. this program does not work properly for me as written.. when I comes time for the second scanner instance the program terminates. I did get working by using a new Scanner (myScanner2) for the String.
Code :
do
{
System.out.print("Enter your guess: ");
userGuessedNumber = myScanner.nextInt();
if(userGuessedNumber > 10 || userGuessedNumber < 1)
{
System.out.println("Enter between 1-10 please.");
}
}while(userGuessedNumber > 10 || userGuessedNumber < 1);
}
System.out.print("Do you want another game? ");
userContinue = myScanner.nextLine();
Re: Question About Using Scanner Object Twice
If you're going to post code, please make sure it's readable, and please explain what you want it to do.
Forget two scanners for now - as I said, you probably don't need two scanners.
For the last time, what are you trying to do?
Re: Question About Using Scanner Object Twice
In this example I was trying to get input from the user. First an int that would be stored in a variable. Later on, but in the same method I want to get a String from the user stored in another variable. This program, as written above, terminates when it comes time for the user to input the String. The only way I got it to work was to use a second Scanner for the String input.
This led me to believe that the problem was data remaining in the first Scanner. As I mentioned above I tried to flush it out the only way I know how but it didn't appear to make a difference.
Re: Question About Using Scanner Object Twice
OK. You need to read the Scanner API docs more carefully.
You haven't posted any indication of how your scanner is set up, but I assume it's using the default delimiter (whitespace). Scanner.nextInt() reads the next token and tries to interpret it as an int. Reading the next token involves reading the first sequence that isn't whitespace (i.e. skipping any whitespace). The Scanner.nextLine() method docs say this: "Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.".
When the user enters a number, they hit 'Enter' to enter it. This puts a line separator after the number in the stream the scanner is reading. The nextInt() method will skip any line separators when getting its token because they are considered whitespace, so it skips to the number and reads it, leaving the scanner on the same line, positioned just before the line separator. A subsequent nextLine() call will return an empty String because it returns everything up to the beginning of the next line except the line separator.
The solution for you is to call Scanner.nextLine() before prompting for another game, so that you advance the scanner past the line separator and to the start of the next line ready to get the next user input.
Re: Question About Using Scanner Object Twice
Thank you that's exactly what I was looking for.
Re: Question About Using Scanner Object Twice