问题
Program uses a while loop menu in the main to request for the user command:
public static void main(String[] args)throws Exception
{
Boolean meow = true;
while(meow)
{
System.out.println("\n 1. Show all records.\n"
+ " 2. Delete the current record.\n"
+ " 3. Change the first name in the current record.\n"
+ " 4. Change the last name in the current record.\n"
+ " 5. Add a new record.\n"
+ " 6. Change the phone number in the current record.\n"
+ " 7. Add a deposit to the current balance in the current record.\n"
+ " 8. Make a withdrawal from the current record if sufficient funds are available.\n"
+ " 9. Select a record from the record list to become the current record.\n"
+ " 10. Quit.\n");
System.out.println("Enter a command from the list above (q to quit): ");
answer = scan.nextLine();
cmd.command(answer);
if(answer.equalsIgnoreCase("10") || answer.equalsIgnoreCase("q"))
{
meow = false;
}
}
}
If none of the commands you pick are actually commands on the menu then this happens:
else
{
System.out.println("Illegal command");
System.out.println("Enter a command from the list above (q to quit): ");
answer = scan.nextLine();
command(answer);
}
Whenever I add a new person or use any command that requires me to press return to finish entering a value I get the else statement and then the regular command request.
So it looks like:
Enter a command from the list above (q to quit):
Illegal command
Enter a command from the list above (q to quit):
When this happens.
Not gonna post my full code on here, I'm afraid of it cause it's so much. Have the pastebins of them instead.
- My full Main Class: http://pastebin.com/rUuKtpXb
- My full not Main Class: http://pastebin.com/UE4H76Cd
Anyone know why this is happening?
回答1:
The problem is that something like Scanner::nextDouble
doesn't read the new-line character, so the next Scanner::nextLine
returns an empty line.
Replacing all occurrences of Scanner::nextLine
with Scanner::next
should fix it.
You can also do a Scanner::nextLine
after your last non-nextLine
next method, but this is a bit messy.
Some other things I'd recommend:
Add
scan.useDelimiter("\n");
at the beginning of your program, test with adding spaces to a line and you'll see why this is needed.Change
println
toprint
, so the command can be entered on the same line. i.e.:Change
System.out.println("Enter a command from the list above (q to quit): ");`
to
System.out.print("Enter a command from the list above (q to quit): ");
Change this:
else { System.out.println("Illegal command"); System.out.println("Enter a command from the list above (q to quit): "); answer = scan.nextLine(); command(answer); }
to:
else System.out.println("Illegal command");
You would print the menu again, but you would avoid unneeded recursion. It would be easy enough to avoid printing the menu again.
It would be better to check for exit before running
command
(and then you can remove that check incommand
).System.out.println("Enter a command from the list above (q to quit): "); answer = scan.nextLine(); if (answer.equalsIgnoreCase("10") || answer.equalsIgnoreCase("q")) meow = false; else cmd.command(answer);
Change
Boolean
toboolean
.Boolean
is the wrapper class forboolean
, which is unneeded in this case.
回答2:
Maybe its leaving \n
in the buffer at the end of the while loop and just before taking input again.
Maybe
while(meow)
{
scan.nextLine();
This can help remove it.
来源:https://stackoverflow.com/questions/15679399/i-made-this-program-and-it-prints-one-extra-thing