Hi guys/gals,

I am student at University of Phoenix. I am not here to have you do my homework, I know this is not the place for that. I do need help though. I am trying to work through this course an really learn something.

Many of you ar probably familiar with the Payroll program part 2. I completed last week's pat one and did well. For this week's part of the program is to modify the previous week's assignment to continue to requesting employee information until the user enters stop as the employee name. It is also suppose to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.

I asked my insructor for some guidance to which he said all you have to do is add a while loop to keep prompting for the employee information and then a couple of if statements to ensure that only positive numbers are entered.

Easy enough but I think it is reiterating what the syllabus tells me to do. I am not sure where in the code the comments are suppose to go or where the while loop goes. Can someone point me in the right direction? Please see my code below.

import java.util.Scanner;


public class WeeklyPay {
public static void main(String args[]) {
// Input using the scanner class
Scanner in = new Scanner(System.in);


// get the Name
System.out.printf("Enter the employee's name:\n");
String name = in.nextLine();


// Rate input
System.out.printf("Enter the employee's hourly rate in US dollars:\n");
double rate = in.nextDouble();


// Hours
System.out.printf("Enter the number of hours worked for the week:\n");
double hours = in.nextDouble();


// Output the result
System.out.printf("Employee: " + name + "\n");
System.out.printf("Pay: $%.2f", rate*hours);
}
}

Recommended Answers

All 4 Replies

I tried this modified code and I think I am on the right track, but I have an error on line 47 stating "reached end of file while parsing } //End While".I am not sure what that means. I am using TextPad by Helios Software to write, edit, compile, and run. Here is my modified code. It has to be something simple I am missing.

//Weeklypay2
import java.util.Scanner; // class Scanner


public class Weeklypay2
{
//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);


String employeeName; // input
double hourlyRate; // input
double hoursWorked; // input
double sum; // weekly pay
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 a positive hourly rate:"); // prompt
hourlyRate = input.nextDouble(); // input
{
while(hourseWorked<0) // same as hourlyRate while loop
{
System.out.print("Enter a positive number of hours worked:"); // prompt
hoursWorked = input.nextDouble(); // input
}
sum = hourlyRate * hoursWorked; // * numbers


System.out.printf("The employee%s was paid $ %.2fthis week", employeeName, sum);


} // end outer while


} // end method main


} // end class Weeklypay2


//Clean the input buffer
cleanInputBuffer=input.nextLine();//Read a line of text to clean the input buffer


} //End while

Okay, did some research and discovered that I have more { than I do } 5 and 6 respectively. Somewhere I am either missing a { opening or I have one too many }. I have been through and toggled these both ways and tried to compile after every one but it returns that same parse error.

Code tags and formatting are vital for problems like these. Without code tags:

[code=JAVA] // paste code here

[/code]

You'll get more help if you use code tags in the future.

//Weeklypay2
import java.util.Scanner; // class Scanner

public class Weeklypay2 
{
//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);

        String employeeName; // input
        double hourlyRate; // input
        double hoursWorked; // input
        double sum; // weekly pay
        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 a positive hourly rate:"); // prompt
                hourlyRate = input.nextDouble(); // input
                {
                    while (hourseWorked < 0) // same as hourlyRate while loop
                    {
                        System.out.print("Enter a positive number of hours worked:"); // prompt
                        hoursWorked = input.nextDouble(); // input
                    }
                    sum = hourlyRate * hoursWorked; // * numbers

                    System.out.printf("The employee%s was paid $ %.2fthis week", employeeName, sum);

                } // end outer while

            } // end method main

        } // end class Weeklypay2

//Clean the input buffer
        cleanInputBuffer = input.nextLine();//Read a line of text to clean the input buffer

    } //End while

These bracket errors are much easier to see when they line up.

Line 49 - note that you have a bracket after line 44, which is where you say your class is finished. Thus if the comment on line 44 is correct, you shouldn't have anything after it. What is the bracket on line 30 supposed to enclose? there's no if or while statement or class or function or anything that would seem to need a bracket.

A few more problems:

cleanInputBuffer = input.nextLine();

cleanInputBuffer is never instantiated.

while (hourseWorked < 0)

"hourseWorked" should be changed to "hoursWorked"

I'm not sure what the bracket on line 27 is used for and the corresponding one is on line 42. I think you could get rid of them. Try formatting your code in the future; it is much easier to debug and read. Also when I run the program is does not output the way you want it to.

Enter Name of Employee:Testname
Enter a positive hourly rate:10
Enter a positive number of hours worked:10
The employeeTestname was paid $ 100.00this weekEnter Name of Employee:Enter a positive hourly rate:

You need to reexamine your System.out.print statements

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.