Please help me with this for loop with Scanner next() vs nextLine()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • milk242
    New Member
    • Feb 2009
    • 25

    #1

    Please help me with this for loop with Scanner next() vs nextLine()

    Code:
    import java.util.Scanner;
    public class DogDemo
    {
    	public static void main(String[] args)
    	{
    		Dog myDog[] = new Dog[2];
    		Scanner keyboard = new Scanner(System.in);
    		String name, breed;
    		int age;
    		boolean boost;
    		
    		for (int i = 0; i < myDog.length; i++)
    		{
    			System.out.println("Dog " +i + " information.");
    			System.out.println("Dog's name?");
    			name = keyboard.nextLine();
    			System.out.println("Dog's breed?");
    			breed = keyboard.nextLine();
    			System.out.println("Dog's age?");
    			age = keyboard.nextInt();
    			System.out.println("Dog's boostershot (true or false)");
    			boost = keyboard.nextBoolean();
    			/*if (ans == "yes")
    				boost = true;
    			else
    				boost = false;*/
    				
    			myDog[i] = new Dog(name, breed, age, boost);
    		}
    		
    		System.out.println("\nDog's information\n");
    		for (int i = 0; i < myDog.length; i++)
    		{
    			System.out.println("Name: " + myDog[i].getName());
    			System.out.println("Breed: " + myDog[i].getBreed());
    			System.out.println("Age: " + myDog[i].getAge());
    			System.out.println("Booster: " + myDog[i].getBoosterShot());
    			System.out.println();			
    		}
    		
    	}
    }
    Output:
    Code:
    Dog 0 information.
    Dog's name?
    Minnie
    Dog's breed?
    York
    Dog's age?
    2
    Dog's boostershot (true or false)
    true
    Dog 1 information.
    Dog's name?
    Dog's breed?
    My question... why does it skip Dog's name after it loops once? It doesn't allow me to enter the second dog's name just skips to breed. I know it has something to do with Scanner method next() vs nextLine() but I'm not sure. I made the strings read next() before and it worked correctly, but when it comes time to enter a breed that is two words, it won't take. Thanks!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The nextInt() and nextBoolean() etc. methods don't read beyond what they have to read. If, say, an int has to be read and you type 123<enter> the int 123 is read and the newline character (caused by the <enter> key) is left in the input buffer.

    If you want to read an entire line afterwards, a new line character is seen and an empty line is returned. This looks as if nothing is read. The cure is simple: add a spurious readLine() method that gets rid of that one new line character and brings you 'in sync' again. Remember that the <enter> key puts a new line character in the input buffer and you have to deal with it one way or another.

    kind regards,

    Jos

    Comment

    • milk242
      New Member
      • Feb 2009
      • 25

      #3
      Thanks! that was a great explanation. That really helped me.

      Comment

      Working...