Results 1 to 3 of 3
  1. #1
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Unhappy 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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: That old problem with scanner and end of line (enter)

    Try input.nextLine().

  3. #3
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default Re: That old problem with scanner and end of line (enter)

    Thanks, I tried that too.
    Luckily I have been given clearance to use input dialog instead.

Similar Threads

  1. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  2. Scanner problem.
    By keo in forum New To Java
    Replies: 6
    Last Post: 04-07-2011, 08:30 AM
  3. enter key problem
    By masa in forum AWT / Swing
    Replies: 3
    Last Post: 03-19-2009, 07:30 AM
  4. How to make Scanner read the same line
    By mcollins in forum New To Java
    Replies: 2
    Last Post: 03-03-2009, 06:41 AM
  5. Reading a line from console using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 11:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •