473,750 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Errors compiling a Payroll Program in Java

4 New Member
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:1 8: illegal start of type
while ( !exit )
^
Payroll3.java:1 8: <identifier> expected
while ( !exit )
^
Payroll3.java:5 3: illegal start of expression
public void setHoursWorked( float hours)
^
Payroll3.java:6 1: <identifier> expected
System.out.prin tln( "Enter hourly pay rate: " ); // prompt for input
^
Payroll3.java:6 1: illegal start of type
System.out.prin tln( "Enter hourly pay rate: " ); // prompt for input
^
Payroll3.java:6 2: <identifier> expected
PayRate = input.nextFloat (); // display hourly rate
^
Payroll3.java:6 3: illegal start of type
while ( PayRate <= 0 ) // prompt the user to input a positive number
^
Payroll3.java:6 3: <identifier> expected
while ( PayRate <= 0 ) // prompt the user to input a positive number
^
Payroll3.java:8 1: <identifier> expected
System.out.prin t( "Total weekly pay is $%.2f\n", multiply); // display
weekly pay
^
Payroll3.java:8 1: illegal start of type
System.out.prin t( "Total weekly pay is $%.2f\n", multiply); // display
weekly pay
^
Payroll3.java:8 2: <identifier> expected
cleanInputBuffe r = input.nextLine( );
^
Payroll3.java:8 5: class, interface, or enum expected
System.out.prin tln(); // inserts a blank line
^
Payroll3.java:8 7: 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 5436
JosAH
11,448 Recognized Expert MVP
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
aureao4
4 New Member
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:4 6: 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.prin t( "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.prin t( "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 Recognized Expert MVP
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:4 6: 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
aureao4
4 New Member
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 Recognized Expert MVP
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
aureao4
4 New Member
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
8664
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 sure if they would be correct anyways. Below is the instructions. and below that is what I have. Any tips or hints would be highly appreciated. Regards, Kelsey
0
1753
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 .bmp files in the same folder as the script. I run my .py script (from IDLE) and all the backgrounds are loaded as intended. in my script I load the images like this:
10
7660
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 employee information, and a method within that class to calculate the weekly pay. It seems that the more I read into this, the more confused I get. Any help will be greatly apreciated. Here is my latest code... // Payroll program that...
5
11626
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 hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered as the employee name, the application should terminate. Her's what I...
1
1618
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 February 2007 Payroll. How will the user close January payroll and then start that of February? I cannot figure out how to write the Program for that. Please kindly help me. Thank you. Akinyemi.
6
10444
by: Mayur1234 | last post by:
Hi all, Can any body tell me how to configure Textpad for compiling and running java application
6
2876
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
16732
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 that calculates weekly pay // sentinel-controlled repetition import java.util.Scanner; // program uses class Scanner public class payroll
0
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.