Click here to Skip to main content
14,872,753 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to write a program that takes a name and amount of money and sends a short thankyou letter and loops until nextLine is "quit"

Its looping just fine, but for some reason on the second loop the program skips the String enter and goes straight to Int..

boolean keepgoing = true;

while (keepgoing = true)

out.println("What is the persons name?");

String personname = keyboard.nextLine();

out.println("How much did they donate?");

int donation = keyboard.nextInt();

out.println(" Dear " + personname + "");
out.println("Thankyou for your donation of " + donation + "\n");

out.println("Regards, James");


keepgoing = false;

What I have tried:

No idea what could be the problem, this seems like a strange quirk.
Updated 2-Oct-17 21:04pm

1 solution

Firstly, why are you processing the name, if it's "quit" anyway? That isn't a "real" name, it's a command to your program - so move your conditional test to immediately after you input the name, and check there.
If it is a "genuine" name, continue to get the donation. But if it isn't, exit the loop.

Secondly, why doesn't that work? Well ... it's the nextInt call that causes it - it leaves the newline that terminates the number on the input stream, so your next call to nextLine retrieves a blank line instead of the user input. Add a nextLine call after the nextInt to remove it, and you are fixed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900