Errors compiling a Payroll Program in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aureao4
    New Member
    • Jul 2007
    • 4

    #1

    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: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:
    [CODE=java] import java.util.Scann er; // program uses Scanner

    public class Payroll3
    {
    private String nameOfEmployee;

    public Payroll3( String name )
    {
    nameOfEmployee = name;
    } // end constructor

    boolean exit = false; // this flag will stop the program

    // loop until user exits from program
    while ( !exit )
    {
    // create Scanner to obtain input from command window
    Scanner input = new Scanner( System.in );

    float HoursWorked; // input number of hours worked
    float PayRate; // input hourly pay rate
    float multiply; // multiply NumberHours with PayRate

    // clean input buffer
    String cleanInputBuffe r = "";

    // input the employee's name
    System.out.prin t( "Enter the employee's name or exit to quit:" );
    String nameOfEmployee = input.nextLine( ); // display employee's name

    if(nameOfEmploy ee.equals("exit "))
    {
    System.out.prin tln( "End of Program.");
    exit = true;
    } // end if statement

    else
    {
    // 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

    //set hours worked
    public void setHoursWorked( float hours )
    {
    HoursWorked = HoursWorked;
    }
    public float getHoursWorked( ) // method get hours worked
    {
    return HoursWorked;
    }
    System.out.prin t( "Enter hourly pay rate: " ); // prompt for input
    PayRate = input.nextFloat (); // display hourly rate
    while ( PayRate <= 0 ) // prompt the user to input a positive number
    {
    System.out.prin t( "Pay Rate must be a positive value." + "Enter hourly pay rate: " ); // prompt for a positive number
    PayRate = input.nextFloat (); // display hourly rate
    } // end while
    public void setPayRate( float PayRate ) // method set pay rate
    {
    PayRate = PayRate;
    }
    public float getPayRate() // method get pay rate
    {
    return PayRate;
    }
    public float calculateWeekly Pay()
    {
    return multiply = HoursWorked * PayRate; //calculate weekly pay
    }

    System.out.prin t( "Total weekly pay is $%.2f\n", multiply); // display weekly pay
    cleanInputBuffe r = input.nextLine( );
    } // end else statement

    System.out.prin tln(); // inserts a blank line

    } // end while

    } // end main

    } // end class Payroll3
    and
    import java.util.Scann er;

    public class TestPayroll3
    { // start Payroll3

    public static void main( String args[])
    {
    Scanner input = new Scanner( System.in );


    TestPayroll3 myEmployee = new TestPayroll3();


    System.out.prin tf("Enter the employee's name or exit to quit:");


    System.out.prin t( "Enter hourly pay rate: " );
    myEmployee.setp ayrate(input.ne xtDouble());


    System.out.prin t( "Please enter the number of hours worked again:" );
    myEmployee.seth oursworked(inpu t.nextDouble()) ;


    System.out.prin tf("Employee earned " + myEmployee.calc ulateweeklypay( ) );


    } // end main
    } // end Payroll3[/CODE]
    Last edited by r035198x; Jul 8 '07, 06:43 AM. Reason: added code tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #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:

    [code=java]
    public void yourMethod() { // starting from here you can put statements

    while( ... ) { // your while statement
    ...
    } // end of your while statement

    } // end of the method, back at class level again
    [/code]

    kind regards,

    Jos

    Comment

    • aureao4
      New Member
      • Jul 2007
      • 4

      #3
      Originally posted by JosAH
      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:

      [code=java]
      public void yourMethod() { // starting from here you can put statements

      while( ... ) { // your while statement
      ...
      } // end of your while statement

      } // end of the method, back at class level again
      [/code]

      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

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by aureao4
        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:

        [code=java]
        class YourClass {
        public void yetAntotherMeth od() {
        // statements here
        }
        public void anotherMethod() {
        // more statements here
        }
        }
        [/code]

        When you forget a right curly bracket you get this:

        [code=java]
        class YourClass {
        public void yetAntotherMeth od() {
        // statements here

        public void anotherMethod() {
        // more statements here
        }
        }
        [/code]

        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

        Comment

        • aureao4
          New Member
          • Jul 2007
          • 4

          #5
          Originally posted by JosAH
          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:

          [code=java]
          class YourClass {
          public void yetAntotherMeth od() {
          // statements here
          }
          public void anotherMethod() {
          // more statements here
          }
          }
          [/code]

          When you forget a right curly bracket you get this:

          [code=java]
          class YourClass {
          public void yetAntotherMeth od() {
          // statements here

          public void anotherMethod() {
          // more statements here
          }
          }
          [/code]

          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

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by aureao4
            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

            Comment

            • aureao4
              New Member
              • Jul 2007
              • 4

              #7
              Originally posted by JosAH
              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

              Comment

              Working...