Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with simple repetition statement

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with simple repetition statement

    I cannot seem to understand why after the first round of input, the loop continues before the name variable is entered the second time. After the loops completes once, I get this output:

    Enter the employee's name: Enter the employee's wage:

    here is the code....
    package payroll;
     
    import java.util.Scanner;                                               
     
    public class Payroll {                                                  
     
        public static void main( String[] args )                              
        {
            Scanner input = new Scanner( System.in );                       
                String name;                                                 
                double wage;                                                
                int hours;                                                        
                double pay;                                                     
     
     
            System.out.print( "Welcome to the Payroll Calculator\n\n" );       
     
                System.out.print( "Enter the employee's name: " );                     
                   name = input.nextLine();  
     
            while (name.compareTo("stop") != 0 )
            {
     
                System.out.print( "Enter the employee's wage: " );             
                    wage = input.nextDouble();                             
                System.out.print( "Enter the number of hours worked: " );    
                    hours = input.nextInt();                               
                pay = wage * hours;                                          
     
     
                System.out.printf( "\n\nName: %s\nWage: $%.2f\nHours worked: %d\nPay: $%.2f\n", name, wage, hours, pay );
     
                System.out.print( "Enter the employee's name: " );            
                   name = input.nextLine();                                 
            }
     }                                                              
    }
    Last edited by helloworld922; February 24th, 2012 at 07:10 PM. Reason: please use [code] tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,557
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Need help with simple repetition statement

    I think your problem could be with how the Scanner class works. It reads the line when you type in something and press Enter and saves all of it in a buffer, including the end-line character. When you call nextInt() the method returns the data you entered and leaves the end-line character in the buffer.
    You need to call the nextLine() method to read the end-line out of the buffer after you call the nextInt().

    It is possible to type many data values on a line before you press Enter and have nextInt() read them one by one until it gets to the end-line character.

    With your current program try entering the wage and the hours worked and the next name all on the same line before pressing Enter and see what happens.

Similar Threads

  1. [SOLVED] (Simple) How to use an if statement to respond to random integers
    By DusteroftheCentury in forum Loops & Control Statements
    Replies: 9
    Last Post: January 27th, 2012, 08:15 PM
  2. Help adding non repetition.
    By arkaneraven in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 15th, 2011, 09:31 PM
  3. [SOLVED] stuck on an assignment and need help with repetition
    By teeej86 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 20th, 2011, 07:03 PM
  4. [SOLVED] Using a For loop for repetition
    By cb5950 in forum Loops & Control Statements
    Replies: 7
    Last Post: March 8th, 2011, 04:53 PM
  5. [SOLVED] changing character repetition
    By vendetta in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 16th, 2010, 05:16 PM