• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code problems

 
Aretha Clark
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the midst of writing the code for my java program, i have seem to taken a misturn somewhere. Any advise or sugestions on what i can do to correct this is much needed. It works good until i get to the employee class near the bottom.

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

public class Payroll {

public Payroll() {

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

String employeeName = ""; //input employee name
double hourlyWage;
double hoursWorked;
double weeklyPay;

// loop until the user enters 'stop' as the name of the employee.
do {

// Get Employee Name
System.out.print("Enter Name of Employee:");
employeeName = input.next();
// if the employee name is 'stop', then exit the loop.
if (employeeName.equals("stop")) {
break;

}

// ask for hourly wage
System.out.print("Enter hourly Wage:");
hourlyWage = input.nextDouble();
// require that the hourly wage is a non-negative
while (hourlyWage < 0.0)
{
System.out.print("Hourly wage must be a positive number. Please enter a positive hourly wage:");//prompt
hourlyWage = input.nextDouble();
input.nextLine();

}

// ask the hours worked
System.out.print("Enter hours worked:");
hoursWorked = input.nextDouble();
// require that hours worked be a non-negative number
while (hoursWorked < 0.0)
{
System.out.print("Hours worked must be a positive number. Please enter a positive number of hours:");//prompt
hoursWorked=input.nextDouble();

input.nextLine();
}


weeklyPay = hourlyWage * hoursWorked;
System.out.printf("The employee, %s, was paid $ %.2f this week.\n\n", employeeName, weeklyPay);

} while (true);


}
} public static void main(String args[]) {
new Payroll();

} //end method main

//end class Payroll



public class Employee {

public Employee() {

String name;
int hoursworked;
int hourlywage;
int weeklyPay;

public Employee(String name, int hours, int rate) {
this.name = name;
this.hoursworked = hours;
hourlywage = rate;
computeWeeklyPay();
}

private void computeWeeklyPay() {
weeklyPay = hoursworked * hourlywage;
}

public int getWeeklyPay() {
return weeklyPay;
}
;
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is the problem? does it compile? does it run? does it run and give output different from what you expected?
 
Aretha Clark
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error i recieved was ..

Payroll.java:92: reached end of file while parsing




[GDB - Editing to use UBB Code Tags ]
[ June 24, 2008: Message edited by: Gregg Bolinger ]
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The error i recieved was ..

Payroll.java:92: reached end of file while parsing


This is generally a sign that you forgot to close one of your braces. The compiler is still expecting more input -- and is seeing your class definitions as ending prematurely. I would recommend that you check you braces to make sure that they are balanced, and correct.

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags.

Have you got a text editor which supports syntax highlighting? If so, use that and see whether you can pair the very last } with the very first {. That sort of error might be caused by incorrectly paired {}.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't try compiling pages of code. Write three lines, compile it, wrtie three more lines and compile it. That way you reduce the amount of code you have to check.
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags.


Unfortunately, I am not sure if it will work here -- I took a quick look to add the code tags. It looks like the code actually doesn't contain any indentations. It actually looks the same with or without code tags.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just looking ahead -- as I am anticipating the next problem.

Is the definition of the Employee class part of the Payroll.java file? Or is it defined in the Employee.java file?

Henry
 
Aretha Clark
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im using txtpad so i not sure
Have you got a text editor which supports syntax highlighting?


However i have managed to get less errors . Im just not sure if im making progress or moving backwards

 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im using txtpad so i not sure


Be sure. It only take a few minutes to do it by hand -- and to correctly indent. You don't want to waste hours trying to hunt down an error message, that could have been avoided by a few minutes of work.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TextPad enabled syntax highlighting but you have to enable it first. You will have to create a document class with members *.java and specify the syntax file for Java. If you don't have that pre-installed check www.textpad.com.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just by looking at the conversation and the program posted by OP, those two are public classes. So definitely they should have been in two different .java files if Aretha Clerk was able to compile them successfully.

One more suggestion Aretha. Don't overload the constructor to do all your business logic. You can very well move the entire process of getting inputs and populating the employee object inside a separate method say, 'getInput()' and you do call it explicitly. It would make the code clean.

Also, having a check on the employee name as 'stop' is not that good. Rather you can have a traditional 'yes/no' type option and conditional looping accordingly!

If you use Notepad++ a little better and enhanced editor, it will highlight you the braces along with the "+" option to collapse / expand the set of codes inside a curly brace pair, with which you can easily identify the missing curly brace if any.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic