• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code problems

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
;
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic