Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Question About Using Scanner Object Twice

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Location
    Edmonton, Canada
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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
    myScanner.next();
    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?


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Question About Using Scanner Object Twice

    Quote Originally Posted by derekeverett View Post
    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.

    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
    myScanner.next();
    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.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Location
    Edmonton, Canada
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.
    			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();

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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?

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Location
    Edmonton, Canada
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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.

  7. The Following User Says Thank You to dlorde For This Useful Post:

    derekeverett (July 16th, 2011)

  8. #7
    Junior Member
    Join Date
    Mar 2010
    Location
    Edmonton, Canada
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Question About Using Scanner Object Twice

    Thank you that's exactly what I was looking for.

  9. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Question About Using Scanner Object Twice

    You're welcome

Similar Threads

  1. Newb question: Using the same object over two methods?
    By CitizenSmif in forum Object Oriented Programming
    Replies: 5
    Last Post: July 3rd, 2011, 09:17 PM
  2. Calling from one javabean object to other javabean object in a single class
    By kichkich in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2011, 08:20 AM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 07:11 AM
  4. Question concerning nextLine() method in Scanner Class
    By r_sardinas in forum Java SE APIs
    Replies: 2
    Last Post: October 20th, 2010, 09:35 PM
  5. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 03:33 PM