473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 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 5417
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

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

Similar topics

1
by: Randi | last post by:
Hi, Looking for some help with this payrool project I have for class. This is what the instructor asks for so far. I have it working without errors but am getting some funky numbers. I am not...
0
by: erkidevries | last post by:
Problem compiling Tkinter program with bmp images (using py2exe) I have a Tkinter gui program that uses bmp as backgrounds. The program itself works when I run from the source. I placed the...
10
by: ycg0771 | last post by:
I'm trying to modify the following program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the...
5
by: jbailey006 | last post by:
I am trying to add the following information to a payroll program I am working on for assignment. Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the...
1
by: Akinyemi | last post by:
I have almost finished writing my Payroll Program. But I am wondering how the program can be used for different months. For example, after, say January 2007 Payroll, the user would want to prepare...
6
by: Mayur1234 | last post by:
Hi all, Can any body tell me how to configure Textpad for compiling and running java application
6
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
2
by: alexlunar | last post by:
Hello, I am trying to figure out when I have done wrong or what i have not included in my most recent edit of my payroll code. My code from last week was // Week 2 payroll // payroll program...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.