Here is what I have so far:

package payrollpart2;

/**
 *
 * @author Darryl
 */

import java.util.Scanner;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        //declare program variables
        String employeeName;
        double hourlyRate;
        double hoursWorked;
        double sum;
        boolean isValid = true;  //by default a boolean is always false

        while(isValid)
        {
            //prompt and accept the student name
            System.out.println("Please enter employee's name or enter stop to exit:");
            employeeName = input.next();

            if(!employeeName.equalsIgnoreCase("Stop"))
            {
            
                //prompt and accept for the first grade
                System.out.println("Please enter employee's hourly rate: $");
                hourlyRate = input.nextDouble();
               // if(grade1 < 0)  //error checking
                while(hourlyRate<0)
                {
                   System.out.println("Employee's hourly rate must be a positive number.  Please re-ener:");
                   hourlyRate = input.nextDouble();
                }

                //prompt and accept for the first grade
                System.out.println("Please enter employee's hours worked: ");
                hoursWorked = input.nextDouble();

                if(hoursWorked < 0)  //error checking
                {
                   System.out.println("Employee's hours worked must be a positive number.  Please re-ener:");
                   hoursWorked = input.nextDouble();
                }
                sum = hourlyRate * hoursWorked;
                System.out.printf("Employee Name: %s\n", employeeName); //display employee name
                System.out.printf( "Hourly Rate: $%.2f\n", hourlyRate ); //display hourly pay rate
                System.out.printf( "Hours Worked: %.2f\n", hoursWorked ); //display hours worked for the week
                System.out.printf( "Weekly Pay: $%.2f\n", sum ); // display sum;
            }
            else
            {
                isValid = false;
                System.out.println("Thank you and Goodbye");

            } //end of else

        } //end of while(isValid)

    }

}

This program compiles successfully on netbeans, but you can only enter the first name. I want it to be able to enter the full name.

In line 30 I changed the: employeeName = input.next();

to: employeeName = input.nextLine();

Although this does allow me to enter the full name, it ends up skipping the prompt for employee name the next time through the loop. I can't seem to figure out how to get it to work. I appreciate any help that anyone can provide.

Recommended Answers

All 4 Replies

The Scanner class is tricky to use.
The last next...() method you call does not read the newline char at the end of the line. It is still in the buffer. When you call nextLine() it reads that newline character and returns an empty string. You need to "eat" this end of input line by calling nextLine() before prompting for the next name.

Hello Norm,

Thanks for the quick reply I really appreciate it. It really helped, it was a really simple fix. Here is the final code that I have, hopefully it will help others with similar problems.

package payrollpart2;

/**
 *
 * @author Darryl
 */

import java.util.Scanner;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        //declare program variables
        String employeeName;
        float hourlyRate;
        float hoursWorked;
        float sum;
        boolean isValid = true;  //by default a boolean is always false

        while(isValid)
        {
            //prompt and accept the student name
            System.out.println("Please enter employee's name or enter stop to exit:");
            employeeName = input.nextLine();

            if(!employeeName.equalsIgnoreCase("Stop"))
            {
            
                //prompt and accept for the first grade
                System.out.println("Please enter employee's hourly rate: $");
                hourlyRate = input.nextFloat();
               // if(grade1 < 0)  //error checking
                while(hourlyRate<0)
                {
                   System.out.println("Employee's hourly rate must be a positive number.  Please re-enter:");
                   hourlyRate = input.nextFloat();
                }

                //prompt and accept for the first grade
                System.out.println("Please enter employee's hours worked: ");
                hoursWorked = input.nextFloat();

                if(hoursWorked < 0)  //error checking
                {
                   System.out.println("Employee's hours worked must be a positive number.  Please re-enter:");
                   hoursWorked = input.nextFloat();
                }
                sum = hourlyRate * hoursWorked;
                System.out.printf("Employee Name: %s\n", employeeName); //display employee name
                System.out.printf( "Hourly Rate: $%.2f\n", hourlyRate ); //display hourly pay rate
                System.out.printf( "Hours Worked: %.2f\n", hoursWorked ); //display hours worked for the week
                System.out.printf( "Weekly Pay: $%.2f\n", sum ); // display sum;

                employeeName = input.nextLine();

            }
            else
            {
                isValid = false;
                System.out.println("Thank you and Goodbye");

            } //end of else

        } //end of while(isValid)

    }

}

You should put a comment on the line that "eats" the end of line vs leaving it to look like its doing something useful.

it will help others with similar problems

ok will do, thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.