Results 1 to 3 of 3
- 09-27-2011, 09:09 AM #1
That old problem with scanner and end of line (enter)
Hello everyone! I am pretty proud of this little program, but I can't fix the problem in the blue area of the program. The user has to hit enter twice in order for his input to be accepted. I've been reading about it everywhere and how to fix it but I have been unable to fix it. I tried putting the loop surrounding the problem in the main method, I've tried a nested if- else method instead of a do loop, I tried using a delimiter, I tried using nextLine instead of nextInt, but nothing makes it go away. Please use your wisdom to help me solve this problem!
Java Code:import javax.swing.JOptionPane; import java.util.Scanner; public class FastFood { static double price=0; static boolean done=false; public static void main (String [] args) { intro(); order(); checkOut(); } static void intro() { JOptionPane.showMessageDialog(null, "Prepare to order!"); } static void order() { Scanner input=new Scanner(System.in); Scanner rage=new Scanner(System.in); /* this scanner was created just to try to solve the problem but it only made it worse so it's just there now */ [COLOR="#0000FF"]do { JOptionPane.showMessageDialog(null, " Enter a number \n\r \n\r (1) Cheeseburger 4.99 \n\r (2) Pepsi 2.00 \n\r (3) Chips 0.75 \n\r (0) Exit"); int choice=input.nextInt(); /* String fury=rage.next(); I tried to "swallow" the enter key with this one but it only doubled the problem so I commented it out */ switch(choice) { case 1: price+=4.99; break; case 2: price+=2.00; break; case 3: price+=0.75; break; case 0: done=true; break; }} while(done==false); } [/COLOR] static void checkOut() { JOptionPane.showMessageDialog(null, "Total Price: "+price +"\n\r Enjoy your food"); } }Last edited by Java Riser; 09-27-2011 at 09:12 AM.
- 09-27-2011, 09:35 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: That old problem with scanner and end of line (enter)
Try input.nextLine().
- 09-29-2011, 01:54 AM #3
Similar Threads
-
Open a URL and read it line by line (Works in Eclipse but not from Command Line)
By rosco544 in forum NetworkingReplies: 16Last Post: 09-17-2011, 02:41 AM -
Scanner problem.
By keo in forum New To JavaReplies: 6Last Post: 04-07-2011, 08:30 AM -
enter key problem
By masa in forum AWT / SwingReplies: 3Last Post: 03-19-2009, 07:30 AM -
How to make Scanner read the same line
By mcollins in forum New To JavaReplies: 2Last Post: 03-03-2009, 06:41 AM -
Reading a line from console using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks