// I keep getting an error when it come time to input the hourly 
//rate. My code seems correct....anyone see what's wrong?? I can't 
//compile the rest of the code then either, which if frustrating 
//because I just put in some loops, and I'm not sure if they are 
//correct either.....any input would be fantastic:)
// A calculation program that displays an employee's information and hourly rate.
import java.util.Scanner;
import java.text.NumberFormat;

public class Payroll
{
    // main method begins execution of Java application
    public static void main( String args[] )
    {

        // create Scanner to obtain input from command window
        Scanner input = new Scanner( System.in );
        NumberFormat currency = NumberFormat.getCurrencyInstance();

        String employeefirstName ; // employee's  first name
        String employeelastName ; // employee's last name
        double hourly_rate; // hourly rate
        double hours_worked; // number of hours worked for the week
        double weekly_pay; // amount due to employee
        boolean end = false; // is the input name stop?        
        while (end == false) // as long as end is false, proceed
        {            
            hourlyRate = -1; // this way, both are initiated to be -1;            
            hoursWorked = -1;            
            System.out.print("Enter Name of Employee:");            
            employeeName = input.nextLine();            
            if (employeeName.toLowerCase() == "stop")             {                
                end = true;            
         } // when the stop is detected, change the boolean, which will end the while loop while(hourlyRate<0) 
           // since this was initiated to -1, it will loop until it gets a positive value
        System.out.print( "Enter employee's first name or type STOP to quit program: " ); // prompt user to input employee's first name
        employeefirstName = input.next();// read employee's name from user's input

        // start loop
        while ( !name.equals("stop") )

            System.out.print( "Enter hourly rate: " ); // prompt user for employee's hourly rate
            hourly_rate = input.nextDouble(); // read hourly rate from user's input
            if (hourly_rate <0){

            System.out.printf( "A negative value was entered for hourly rate, enter a positive rate");
        } else {

            System.out.printf( "Enter hours worked for the week: " ); // prompt user to enter number of hours worked for the week
            hours_worked = input.nextDouble();// read number of hours worked from user's input
            if (hours <= 0) {

            System.out.println("A negative value was entered for the number of hours, enter a positive number");

        }    else {
            weekly_pay = hourly_rate * hours_worked; // calculating weekly pay

        System.out.println( "Employee: " + employeefirstName + employeelastName ); // displays employee's name
        System.out.println( "Total Pay: " + currency.format(weekly_pay)); // displays weekly pay

        //1st prompt for user input in loop
             System.out.println("Enter employee's first name\nor type stop to quit");//
             employeefirstName = input.next();

    } // end method main

 }  // end class Payroll

Recommended Answers

All 2 Replies

Post in code=java tags. And post what error you're getting, we can't really do mind reading.

To be honest with you, your code gives the appearance that you combined your code with someone else's code. Check your variable names. I would recommend to choose a variable naming standard for yourself. You used "hourlyRate" in one place and "hourly_rate" in other places. This gives the appearance that you copied someone else's code and it creates confusion for someone trying to read your code. Also, it means that you have undefined variables.

When doing String comparisons use ".equals", not "==".

Unless you change the Scanner delimiter, you are going to have to use "nextLine" to read (and discard) the "\n" (newline) of each line.

Scanner input = new Scanner( System.in );
input.useDelimiter("\n");
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.