Java programming question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NickiWood
    New Member
    • Jun 2008
    • 3

    Java programming question

    I am in a beginners Java programming class. So far I am doing ok in the class, although there are some things that I am sure will come in time. Right now we are doing an onging project that started with a basic payroll application. Every week we build on it. I am having some troubles with this week. The idea of the program is to continue to request the employees information until the user enters 'stop' as the employee name. I keep getting an error message when I compile it.

    Here is what I have:

    // Payroll22.java
    // Payroll program that displays the weekly pay of employee.
    import java.util.Scann er; // program uses class Scanner // Author: Nicki

    public class Payroll22
    {

    // main method begins execution of Java application
    public static void main( String args[] )

    String name = input.next();
    double rate = input.nextDoubl e();
    double hours = input.nextDoubl e();

    {
    // create Scanner to obtain input from command window
    Scanner input = new Scanner( System.in );

    // prompt for and input employee name
    System.out.prin tf( "Enter the employee name:\n" );

    while (!name.equalsIg noreCase("stop" ))
    {

    // Hourly Rate input
    System.out.prin tf( "Enter the employee's hourly rate in dollars:\n" );

    while ( rate < 0 )
    {
    System.out.prin tf( "Please enter a positive value:" );
    rate = input.nextDoubl e( );
    } // end while loop (pay rate)

    // Hours Worked input
    System.out.prin tf( "Enter the number of hours worked:\n" );

    while ( hours < 0 )
    {
    System.out.prin tf( "Please enter a positive value:" );
    hours = input.nextDoubl e( );
    } // end while loop (hours worked)

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

    // prompt for and input employee name
    System.out.prin tf( "Enter the employee name:" );

    } // end while loop

    } // end method main

    } // end class Payroll22



    The error that shows up has to do with line 9 and it says the following:

    ' ; ' expected public static void main ( String args[] )

    The arrow is pointing to the space after the last parenthesis.

    Any ideas what I could be doing wrong? I have a feeling it is something obvious that I keep overlooking because I have looked at it too long.

    Thank you for your help!

    Nicki
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Your method should look like the following (note braces)

    [CODE=Java]public static void main(String[]args) {
    ...
    }[/CODE]

    Remember that all methods (including main) have a body delimited by braces.

    Comment

    • NickiWood
      New Member
      • Jun 2008
      • 3

      #3
      Thank you very much. I knew it was an obvious mistake.

      Of course now I have 3 additional errors. This time they are within my varibale definitions. It says that the ' i ' in input is a symbol that cannot be found. nay ideas?

      Thanks again!

      Nicki

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        You need to create your Scanner object using its constructor, so include a line at the top:

        [code=java]Scanner input = new Scanner();[/code]

        That's how you create objects (anything that isn't an int/float/double/boolean/etc builtin primitive type).

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          You need to keep in mind that local variables need to be defined before they can be used, and that you should prompt the user before reading their reply: just remember that statements are executed in order:
          [CODE=Java]
          public static void main(String[] args) {
          Scanner input = new Scanner(System. in);

          System.out.prin tf( "Enter the employee name:\n" );
          String name = input.next();
          ...[/CODE]

          Comment

          Working...