Java Help

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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
 
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 );
 

fyrejumper

Thread Starter
Joined
Nov 2, 2007
Messages
4
Chicon said:
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 );

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!!
 
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.
 

fyrejumper

Thread Starter
Joined
Nov 2, 2007
Messages
4
Chicon said:
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.
Chicon,

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

Thanks
 
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]
 

fyrejumper

Thread Starter
Joined
Nov 2, 2007
Messages
4
Chicon said:
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]
Something so simple. I think I'm getting an idea now. Thanks!!!:)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top