java help please!

help with java errors

Page 1 of 1

12 Replies - 1363 Views - Last Post: 23 November 2008 - 08:11 PM Rate Topic: -----

#1 pennybomber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 08

java help please!

Posted 23 November 2008 - 06:47 PM

Hey guys bare with me as this is my first assignment for my Java class. Here is the assignement and my class must be named payroll1. Any help on correcting or pointing out what is wrong with my code would be great.

Create a non-GUI based Java application that calculates weekly pay for an
employee. The application should display text that requests the user input the name of the employee, the hourly rate, and the number of hours worked for that week. The application should then print out the name of the employee and the weekly pay amount. In the printout, display the dollar symbol ($) to the left of the weekly pay amount and format the weekly pay amount to display currency.

// Payroll1.java
// Payroll1 that displays calculates weekly pay for an employee

import java.util.Scanner; // program uses class payroll1
import java.text.NumberFormat; // program uses class NumberFormat
public class Payroll1 {


// main method begins execution of Java application
public static void main(String[] args) {
// create payroll1 to obtain input from command window
payroll1 input = new payroll1( System.in );
NumberFormat currency = new DecimalFormat("\u00A4 #.00");


String fname; // first name to be entered
String lname; // last name to be entered
double hourly; // hourly rate to be entered
int hours; // number of hours worked to be entered
double product; // product of hourly and hours

System.out.print( " Enter the employee's first name: "); // prompt for name
fname = input.nextLine(); // read first name from user

System.out.print( " Enter the employee's last name: "); // prompt for name
lname = input.nextLine(); // read last name from user

System.out.print( " Enter the hourly rate."); // prompt for hourly rate
hourly = input.nextDouble(); //read hourly rate from user

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

product = hourly * hours; // multiply numbers

System.out.println("Employee Name is: " + fname + " " + lname);
System.out.println("Hourly wage is: " + hourly);
System.out.println("Weekly pay is: " + currency.format(product));

} // end method main

} // end class Payroll1

Here are the errors I receive when I run the program in RealJ

C:\IT215\payroll 1\payroll1.java:6: class Payroll1 is public, should be declared in a file named Payroll1.java
public class Payroll1 {
^
.\payroll1.java:6: class Payroll1 is public, should be declared in a file named Payroll1.java
public class Payroll1 {
^
C:\IT215\payroll 1\payroll1.java:12: cannot access payroll1
bad class file: .\payroll1.java
file does not contain class payroll1
Please remove or make sure it appears in the correct subdirectory of the classpath.
payroll1 input = new payroll1( System.in );
^
3 errors
Exit code: 1
There were errors

Is This A Good Question/Topic? 0
  • +

Replies To: java help please!

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: java help please!

Posted 23 November 2008 - 07:18 PM

its difficult to read ur code, can u place it between code brackets

This post has been edited by mostyfriedman: 23 November 2008 - 07:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 pennybomber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 08

Re: java help please!

Posted 23 November 2008 - 07:24 PM

I will try but this is my first assignment with Java code so that partially is the problem as I am not familiar with who code should be wrote properly.
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: java help please!

Posted 23 November 2008 - 07:34 PM

import java.util.Scanner; // program uses class payroll1
import java.text.NumberFormat; // program uses class NumberFormat
public class Payroll1 {


// main method begins execution of Java application
public static void main(String[] args) {
// create payroll1 to obtain input from command window
payroll1 input = new payroll1( System.in );
NumberFormat currency = new DecimalFormat("\u00A4 #.00");


String fname; // first name to be entered
String lname; // last name to be entered
double hourly; // hourly rate to be entered
int hours; // number of hours worked to be entered
double product; // product of hourly and hours

System.out.print( " Enter the employee's first name: "); // prompt for name
fname = input.nextLine(); // read first name from user

System.out.print( " Enter the employee's last name: "); // prompt for name
lname = input.nextLine(); // read last name from user

System.out.print( " Enter the hourly rate."); // prompt for hourly rate
hourly = input.nextDouble(); //read hourly rate from user

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

product = hourly * hours; // multiply numbers

System.out.println("Employee Name is: " + fname + " " + lname);
System.out.println("Hourly wage is: " + hourly);
System.out.println("Weekly pay is: " + currency.format(product));

} // end method main

} // end class Payroll1



ok, for input you should have used scanner
Scanner input = new Scanner(System.in);


This post has been edited by mostyfriedman: 23 November 2008 - 07:36 PM

Was This Post Helpful? 0
  • +
  • -

#5 pennybomber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 08

Re: java help please!

Posted 23 November 2008 - 07:39 PM

Ok I made that last change and now here is the new code becasue I found out I cannot use the NumberFormat currency = new DecimalFormat("\u00A4 #.00");

Your currency should display in $DDD.cc format, with a leading $ and exactly two digits to the right of the decimal point. Do not use DecimalFormat for formatting. It’s “old school” and is the incorrect tool. Use System.out.printf and the appropriate “format control phrase.”



// Payroll1.java
// Payroll1 that displays calculates weekly pay for an employee

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

public class Payroll1
{
// main method begins execution of Java application
public static void main(String[] args) {
// create Scanner to obtain input from command window

Scanner input = new Scanner(System.in);

String fname; // first name to be entered
String lname; // last name to be entered
double hourly; // hourly rate to be entered
int hours; // number of hours worked to be entered
double product; // product of hourly and hours

System.out.print( " Enter the employee's first name: "); // prompt for name

fname = input.nextLine(); // read first name from user

System.out.print( " Enter the employee's last name: "); // prompt for name
lname = input.nextLine(); // read last name from user

System.out.print( " Enter the hourly rate."); // prompt for hourly rate
hourly = input.nextDouble(); //read hourly rate from user

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

product = hourly * hours; // multiply numbers

System.out.println( "Employee Name is: " + fname + " " + lname);
System.out.println( "Hourly wage is: " + hourly);
System.out.printf( "weekly pay is $%d\n" , e.getPay() ); // displays weekly pay
System.out.println();

} // end method main

}
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: java help please!

Posted 23 November 2008 - 07:44 PM

from where are you getting these methods?? getPay(), lol are you copying this code from somewhere?

if you have another class, please post the code for it
Was This Post Helpful? 0
  • +
  • -

#7 pennybomber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 08

Re: java help please!

Posted 23 November 2008 - 07:47 PM

C:\IT215\payroll1>javac payroll1.java
payroll1.java:6: class Payroll1 is public, should be declared in a file named Pa
yroll1.java
public class Payroll1
^
payroll1.java:37: cannot find symbol
symbol : variable e
location: class Payroll1
System.out.printf( "weekly pay is $%d\n" , e.getPay() ); // disp
lays weekly pay
^
2 errors

C:\IT215\payroll1>

View Postmostyfriedman, on 23 Nov, 2008 - 06:44 PM, said:

from where are you getting these methods?? getPay(), lol are you copying this code from somewhere?

if you have another class, please post the code for it

haha yeah I did copy that part but I had a feeling it was not going to work. Im really stuck and my instructor is not available. :( if anyone can please help I would be so greatful. Also my email is pennybomber1@hotmail.com if someone wants to IM me
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: java help please!

Posted 23 November 2008 - 07:48 PM

C:\IT215\payroll1>

Quote

C:\IT215\payroll1>javac payroll1.java
payroll1.java:6: class Payroll1 is public, should be declared in a file named Pa
yroll1.java
public class Payroll1
^
payroll1.java:37: cannot find symbol
symbol : variable e
location: class Payroll1
System.out.printf( "weekly pay is $%d\n" , e.getPay() ); // disp
lays weekly pay
^
2 errors

C:\IT215\payroll1>

you are getting these errors because you haven't declared and initialized a variable named e, also the method getPay() doesnt exit

This post has been edited by mostyfriedman: 23 November 2008 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: java help please!

Posted 23 November 2008 - 07:50 PM

Welcome!

Please post your code examples using - :code:
Was This Post Helpful? 0
  • +
  • -

#10 pennybomber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 08

Re: java help please!

Posted 23 November 2008 - 07:53 PM

// Payroll1.java 
// Payroll1 that displays calculates weekly pay for an employee 

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

public class Payroll1 
{ 
// 	main method begins execution of Java application 
	public static void main(String[] args) { 
// 		create Scanner to obtain input from command window 

		Scanner input = new Scanner(System.in); 

		String fname; // first name to be entered 
		String lname; // last name to be entered 
		double hourly; // hourly rate to be entered 
		int hours; // number of hours worked to be entered 
		double product; // product of hourly and hours 

		System.out.print( " Enter the employee's first name: "); // prompt for name 

		fname = input.nextLine(); // read first name from user 

		System.out.print( " Enter the employee's last name: "); // prompt for name 
		lname = input.nextLine(); // read last name from user 

		System.out.print( " Enter the hourly rate."); // prompt for hourly rate 
		hourly = input.nextDouble(); //read hourly rate from user 

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

		product = hourly * hours; // multiply numbers 

		System.out.println( "Employee Name is: " + fname + " " + lname); 
		System.out.println( "Hourly wage is: " + hourly); 
		System.out.printf( "weekly pay is $%d\n" , e.getPay() ); // displays weekly pay 
		System.out.println();  

	} // end method main 

} 



Was This Post Helpful? 0
  • +
  • -

#11 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: java help please!

Posted 23 November 2008 - 07:54 PM

for the weekly pay, calculate how many hours will he work a day and multiply the wage for them by the number of working days
Was This Post Helpful? 0
  • +
  • -

#12 pennybomber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 08

Re: java help please!

Posted 23 November 2008 - 07:56 PM

View Postmostyfriedman, on 23 Nov, 2008 - 06:54 PM, said:

for the weekly pay, calculate how many hours will he work a day and multiply the wage for them by the number of working days

Right but what does mean:

Your currency should display in $DDD.cc format, with a leading $ and exactly two digits to the right of the decimal point. Do not use DecimalFormat for formatting. It’s “old school” and is the incorrect tool. Use System.out.printf and the appropriate “format control phrase.”
Was This Post Helpful? 0
  • +
  • -

#13 pennybomber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 08

Re: java help please!

Posted 23 November 2008 - 08:11 PM

Turns out I need to learn how to capitalize my folder and file name! Thanks to everyone for the help though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1