aspose file tools*
The moose likes Beginning Java and the fly likes Code problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Code problems" Watch "Code problems" New topic
Author

Code problems

Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
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

Joined: Oct 02, 2003
Posts: 11498
    
  16

What exactly is the problem? does it compile? does it run? does it run and give output different from what you expected?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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

Joined: Mar 07, 2008
Posts: 21
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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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

Joined: Oct 27, 2005
Posts: 19784
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code problems