472,121 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

Errors compiling a Payroll Program in Java

I'm new to Java and programming. I'm trying to code a payroll program and continue getting errors. The program worked last week, this week I have to add set, get and a class. I've written the class but when I compile the program I get 13 errors. Can anyone assist me with this? Thank you!

Errors I get:
C:\Java>javac *.java
Payroll3.java:18: illegal start of type
while ( !exit )
^
Payroll3.java:18: <identifier> expected
while ( !exit )
^
Payroll3.java:53: illegal start of expression
public void setHoursWorked( float hours)
^
Payroll3.java:61: <identifier> expected
System.out.println( "Enter hourly pay rate: " ); // prompt for input
^
Payroll3.java:61: illegal start of type
System.out.println( "Enter hourly pay rate: " ); // prompt for input
^
Payroll3.java:62: <identifier> expected
PayRate = input.nextFloat(); // display hourly rate
^
Payroll3.java:63: illegal start of type
while ( PayRate <= 0 ) // prompt the user to input a positive number
^
Payroll3.java:63: <identifier> expected
while ( PayRate <= 0 ) // prompt the user to input a positive number
^
Payroll3.java:81: <identifier> expected
System.out.print( "Total weekly pay is $%.2f\n", multiply); // display
weekly pay
^
Payroll3.java:81: illegal start of type
System.out.print( "Total weekly pay is $%.2f\n", multiply); // display
weekly pay
^
Payroll3.java:82: <identifier> expected
cleanInputBuffer = input.nextLine();
^
Payroll3.java:85: class, interface, or enum expected
System.out.println(); // inserts a blank line
^
Payroll3.java:87: class, interface, or enum expected
} // end while
^
13 errors



My program is as follows:
Expand|Select|Wrap|Line Numbers
  1.  import java.util.Scanner; // program uses Scanner
  2.  
  3. public class Payroll3
  4. {
  5.  private String nameOfEmployee;
  6.  
  7.   public Payroll3( String name ) 
  8.   { 
  9.     nameOfEmployee = name;
  10.   } // end constructor
  11.  
  12.      boolean exit = false; // this flag will stop the program
  13.  
  14.        // loop until user exits from program
  15.        while ( !exit )
  16.        {
  17.          // create Scanner to obtain input from command window
  18.          Scanner input = new Scanner( System.in );
  19.  
  20.          float HoursWorked; // input number of hours worked
  21.          float PayRate; // input hourly pay rate
  22.          float multiply; // multiply NumberHours with PayRate
  23.  
  24.          // clean input buffer
  25.          String cleanInputBuffer = "";
  26.  
  27.          // input the employee's name
  28.          System.out.print( "Enter the employee's name or exit to quit:" );
  29.          String nameOfEmployee = input.nextLine(); // display employee's name
  30.  
  31.            if(nameOfEmployee.equals("exit"))
  32.            {
  33.               System.out.println( "End of Program.");
  34.               exit = true;
  35.            } // end if statement
  36.  
  37.            else
  38.            {
  39.               // user did not exit, so continue prompt
  40.               System.out.print( "Enter number of hours worked:" ); // prompt for input
  41.               HoursWorked = input.nextFloat(); // display number of hours worked
  42.                 while ( HoursWorked <= 0 ) // prompt the user to input a positive number
  43.              {
  44.                   System.out.print( "Number of hours worked must be a positive value." + "Please enter the number of hours worked again:" ); 
  45.                   // prompt for a positive number
  46.                   HoursWorked = input.nextFloat();
  47.                 } // end while
  48.  
  49.                 //set hours worked
  50.                 public void setHoursWorked( float hours )
  51.                 {
  52.                    HoursWorked = HoursWorked;
  53.                 }
  54.                 public float getHoursWorked() // method get hours worked
  55.                 {
  56.                    return HoursWorked;
  57.                 }
  58.                      System.out.print( "Enter hourly pay rate: " ); // prompt for input
  59.                      PayRate = input.nextFloat(); // display hourly rate
  60.                      while ( PayRate <= 0 ) // prompt the user to input a positive number
  61.                      {
  62.                      System.out.print( "Pay Rate must be a positive value." + "Enter hourly pay rate: " ); // prompt for a positive number
  63.                      PayRate = input.nextFloat(); // display hourly rate
  64.                      } // end while
  65.                    public void setPayRate( float PayRate ) // method set pay rate
  66.                    {
  67.                      PayRate = PayRate;
  68.                    }
  69.                    public float getPayRate() // method get pay rate
  70.                    {
  71.                      return PayRate;
  72.                    }
  73.                         public float calculateWeeklyPay()
  74.                       {
  75.                         return multiply = HoursWorked * PayRate; //calculate weekly pay
  76.                       }
  77.  
  78.                        System.out.print( "Total weekly pay is $%.2f\n", multiply); // display weekly pay
  79.                        cleanInputBuffer = input.nextLine();
  80.             } // end else statement
  81.  
  82.                      System.out.println(); // inserts a blank line
  83.  
  84.         } // end while
  85.  
  86.   } // end main
  87.  
  88. } // end class Payroll3
  89.  and
  90. import java.util.Scanner;
  91.  
  92. public class TestPayroll3 
  93.  { // start Payroll3 
  94.  
  95.   public static void main( String args[]) 
  96.   { 
  97.   Scanner input = new Scanner( System.in ); 
  98.  
  99.  
  100.     TestPayroll3 myEmployee = new TestPayroll3(); 
  101.  
  102.  
  103.     System.out.printf("Enter the employee's name or exit to quit:"); 
  104.  
  105.  
  106.          System.out.print( "Enter hourly pay rate: " ); 
  107.          myEmployee.setpayrate(input.nextDouble()); 
  108.  
  109.  
  110.          System.out.print( "Please enter the number of hours worked again:" ); 
  111.          myEmployee.sethoursworked(input.nextDouble()); 
  112.  
  113.  
  114.          System.out.printf("Employee earned " + myEmployee.calculateweeklypay() ); 
  115.  
  116.  
  117.   } // end main
  118.  } // end Payroll3
Jul 8 '07 #1
6 5253
JosAH
11,448 Expert 8TB
Look at that first while loop; it's a statement. Statements can on occur in a body
of a method. You put that while statement at the class level, i.e. outside of any
method body. It should read something like this:

Expand|Select|Wrap|Line Numbers
  1. public void yourMethod() { // starting from here you can put statements
  2.  
  3.    while( ... ) { // your while statement
  4.       ...
  5.    } // end of your while statement
  6.  
  7. } // end of the method, back at class level again
  8.  
kind regards,

Jos
Jul 8 '07 #2
Look at that first while loop; it's a statement. Statements can on occur in a body
of a method. You put that while statement at the class level, i.e. outside of any
method body. It should read something like this:

Expand|Select|Wrap|Line Numbers
  1. public void yourMethod() { // starting from here you can put statements
  2.  
  3.    while( ... ) { // your while statement
  4.       ...
  5.    } // end of your while statement
  6.  
  7. } // end of the method, back at class level again
  8.  
kind regards,

Jos
Jos, thanks for the advice that worked. But for some reason it won't read the public void setHoursWorked
The error I know get is: Payroll3.java:46: illegal start of expression
public void setHoursWorked( float hours )

Here is the portion of the code where I get the error message:

else
{
// method set hours worked
public void setHoursWorked( float hours )
{
HoursWorked = HoursWorked;
} // end set hours worked
public float getHoursWorked() // method get hours worked
{
return HoursWorked;

// user did not exit, so continue prompt
System.out.print( "Enter number of hours worked:" ); // prompt for input
HoursWorked = input.nextFloat(); // display number of hours worked
while ( HoursWorked <= 0 ) // prompt the user to input a positive number
{
System.out.print( "Number of hours worked must be a positive value." + "Please enter the number of hours worked again:" );
// prompt for a positive number
HoursWorked = input.nextFloat();
} // end while
} // end get hoursworked method
Jul 8 '07 #3
JosAH
11,448 Expert 8TB
Jos, thanks for the advice that worked. But for some reason it won't read the public void setHoursWorked
The error I know get is: Payroll3.java:46: illegal start of expression
public void setHoursWorked( float hours )
Given the error message you have a little (curly) braces problem; the result is
that to the compiler it seems that you want to define another method inside
yet another method; here's a correct example:

Expand|Select|Wrap|Line Numbers
  1. class YourClass {
  2.    public void yetAntotherMethod() {
  3.       // statements here
  4.    }
  5.    public void anotherMethod() {
  6.       // more statements here
  7.    }
  8. }
  9.  
When you forget a right curly bracket you get this:

Expand|Select|Wrap|Line Numbers
  1. class YourClass {
  2.    public void yetAntotherMethod() {
  3.       // statements here
  4.  
  5.       public void anotherMethod() {
  6.          // more statements here
  7.       }
  8. }
  9.  
which is incorrect of course; count the matching curly brackets and most likely
you'll find your error (maybe your editor can count and match them for you).

kind regards,

Jos
Jul 8 '07 #4
Given the error message you have a little (curly) braces problem; the result is
that to the compiler it seems that you want to define another method inside
yet another method; here's a correct example:

Expand|Select|Wrap|Line Numbers
  1. class YourClass {
  2.    public void yetAntotherMethod() {
  3.       // statements here
  4.    }
  5.    public void anotherMethod() {
  6.       // more statements here
  7.    }
  8. }
  9.  
When you forget a right curly bracket you get this:

Expand|Select|Wrap|Line Numbers
  1. class YourClass {
  2.    public void yetAntotherMethod() {
  3.       // statements here
  4.  
  5.       public void anotherMethod() {
  6.          // more statements here
  7.       }
  8. }
  9.  
which is incorrect of course; count the matching curly brackets and most likely
you'll find your error (maybe your editor can count and match them for you).

kind regards,

Jos
Jos, again thank you for the assistance. I counted all the brackets and matched them up. But I can't seem to get rid of the error message. Thanks again! Aurea
Jul 8 '07 #5
JosAH
11,448 Expert 8TB
Jos, again thank you for the assistance. I counted all the brackets and matched them up. But I can't seem to get rid of the error message. Thanks again! Aurea
Well then post your code up to and including the first method the compiler
starts whining about. Put your code in [ code ] ... [ /code ] tags (without the
spaces) for readability reasons.

kind regards,

Jos
Jul 8 '07 #6
Well then post your code up to and including the first method the compiler
starts whining about. Put your code in [ code ] ... [ /code ] tags (without the
spaces) for readability reasons.

kind regards,

Jos
Jos,

I got it to work. I found out I actually needed two files, a class and my program. I played with it until I was able to correct all errors and it finally worked.
Thank you for your assistance.
Aurea
Jul 8 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Randi | last post: by
2 posts views Thread by alexlunar | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.