1

I know this has been asked to death, but I have yet to search and find a case quite like mine so i figured i'd ask...I have this little code here...

System.out.print("What would you like the name of your new recipe to be? ");
        Recipe tempRecipe = new Recipe(name);
        name = scan.nextLine();
        scan.nextLine();
        tempRecipe.setName(name);

        System.out.print("How many ingredients does this recipe have? ");
        ingredientCount = scan.nextInt();
        scan.nextLine();

Now obviously I was running into the issue where the println statements were on the same line and not allowing input, so i threw in that scan.nextLine() to solve the problem. But now the issue is when i read something in it just gives me blank because of too much nextLine()! if I change it to name = scan.next() i can only read in one word and i need to be able to read in both 1 or 2 words indiscriminately if need be, it might also help to know that following this code are these lines

Ingredient tempIngredient = new Ingredient(name, quantity, null);

            System.out.print("Enter the name of ingredient number " + (i+1) + ": ");
            name = scan.nextLine();
            tempIngredient.setName(name);

            System.out.print("Enter the quantity of ingredient number " + (i+1) + ": ");
            quantity = scan.nextDouble();
            scan.nextLine();
            tempIngredient.setQuantity(quantity);

            System.out.print("Enter the unit of measurement of ingredient number " + (i+1) + ": ");
            unit = scan.nextLine();
            tempIngredient.setUnit(UnitOfMeasurement.valueOf(unit));

Both tempRecipes name and tempIngredients name need to be capable of holding 1 or 2 words if need be, how do i do this while still fixing the nextLine() problem!?

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Sherifftwinkie
  • 391
  • 1
  • 13
  • 21

1 Answers1

1

Call the extra scan.nextLine() only after scan.nextInt(), and not after scan.nextLine(). The idea of using the extra scan.nextLine() is to skip to new-line character and go to the next line (since scan.nextInt() does not do that).

See Scanner issue when using nextLine after nextXXX

Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • But then i get an output like this "What would you like the name of your new recipe to be? How many ingredients does this recipe have? " All on one line! – Sherifftwinkie Feb 14 '13 at 01:57
  • @Sherifftwinkie It seems that you used the scanner before `name = scan.nextLine();` and there's still a new-line character in the buffer not consumed. – Eng.Fouad Feb 14 '13 at 02:03
  • optionAnswer = scan.next(); Could that be the culprit? And I thought that wouldnt matter because it is only looking for 1 letter and it will always only get 1 letter as input – Sherifftwinkie Feb 14 '13 at 02:06
  • @Sherifftwinkie `scan.next()` won't consume the new-line character. You have to call extra `scan.nextLine()` after `scan.next()`. – Eng.Fouad Feb 14 '13 at 02:09