1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Java Help

Discussion in 'Software Development' started by fyrejumper, Nov 2, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. fyrejumper

    fyrejumper Thread Starter

    Joined:
    Nov 2, 2007
    Messages:
    4
    Hi!! I'm new to Java and am trying to learn it as best I can. I keep getting an error message: WeeklyPay.java:58: illegal start of expression
    static Scanner sc = new Scanner(System.in);
    ^
    1 error

    I can't figure out where I've messed up. The program is intended to output the weekly pay and then ask the user to enter another name Yes or No and either continue or quit based on user input. Can anyone please assist with this? I have listed the program below.

    Thanks in advance!!

    /**
    * @(#)WeeklyPay.java
    *
    *
    * @
    * @version 1.00 2007/10/23
    */

    import java.util.Scanner; // program uses Scanner

    import java.io.*; // program uses input/output


    public class WeeklyPay

    {
    public static void main( String[] args )

    {
    // prompt the user to enter employee name
    System.out.print("Please enter employee last name, first name: ");

    // open standard input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String employeeName = null;

    // read username from command-line; use try/catch with the readline() method

    try
    {
    employeeName = br.readLine();
    }

    catch (IOException ioe)
    {
    System.out.println("IO error trying to read employee name!");
    System.exit(1);
    }

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

    int x; // employee hourly wage
    int y; // employee hours work for week
    int result; // total weekly pay

    System.out.print( "Enter employees hourly wage: " ); // prompt for hourly wage
    x = input.nextInt(); // read hourly wage

    System.out.print( "Enter employee hours worked for the week: " ); // prompt for hours worked for week
    y = input.nextInt(); // read employee hours

    result = x * y; //calculate weekly pay

    System.out.printf( "Weekly Pay is $%d\n", result ); //output weekly pay

    static Scanner sc = new Scanner(System.in);

    {
    String input = "Y";
    while (input.equals("Y"))
    {
    System.out.println(" ");
    System.out.print
    ("Do you want to enter another employee name?" + " (Y or N)");
    input = sc.next();
    }
    }

    } // end method main

    } //end class WeeklyPay
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi fyrejumper,

    Welcome to TSG ! :)

    - the static modifier is wrong :
    line : static Scanner sc = new Scanner(System.in);

    - The assignment String input = "Y"; is wrong because the line :
    Scanner input = new Scanner( System.in );
     
  3. fyrejumper

    fyrejumper Thread Starter

    Joined:
    Nov 2, 2007
    Messages:
    4

    Thanks for the welcome Chicon. I am still lost on the concept. Remember I am a newbie. :) What do you mean by the static modifier and the assignmnet is wrong?

    Thanks!!
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    In Java, modifiers are terms like public, private, final, abstract and so on ... On this page, you'll find a short summary of Java modifiers and their meaning.
    static variables or fields must be declared outside methods.

    Assignements are not specific to Java language.
    Expressions like :
    String input = "Y"; in Java
    Let A = 15 in Basic
    MOVE 10 TO VARIABLE in Cobol
    ...
    are called assignments; they mean : assign the value something to the variable some_name.
    In your program, you're declaring the same name input for 2 different things : Scanner and String.
     
  5. fyrejumper

    fyrejumper Thread Starter

    Joined:
    Nov 2, 2007
    Messages:
    4
    Chicon,

    I see what your talking about now, I just don't understand how to fix it.

    Thanks
     
  6. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Just replace the second declaration of input by another name, response for example :

    Code:
    [SIZE=2]
    String response = "Y";
    while (response.equals("Y"))
    {
        System.out.println(" ");
        System.out.print("Do you want to enter another employee name?" + " (Y or N)");
        response = sc.next();
    }[/SIZE]
     
  7. fyrejumper

    fyrejumper Thread Starter

    Joined:
    Nov 2, 2007
    Messages:
    4
    Something so simple. I think I'm getting an idea now. Thanks!!!:)
     
  8. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    You're welcome ! :)
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/646704

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice