Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,301 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 777 people online right now. Registration is fast and FREE... Join Now!




payroll program errors

 
Reply to this topicStart new topic

payroll program errors, errors with while and stop

sswearengin
14 Sep, 2007 - 10:31 AM
Post #1

New D.I.C Head
*

Joined: 9 Sep, 2007
Posts: 17


My Contributions
CODE
// Shawn Swearengin
//IT 215
//Axia College of the University of Phoenix
//September 10, 2007

import java.util.Scanner;

public class Main
        
        
{
    
public static void main (String[] args)
{
          Scanner input = new Scanner(System.in);
   String employee="___";
   double hourlyWage=0.0;
   double hoursWorked=0.0;
  

   while(employee != "stop"){
   employee="___";
   System.out.println("\n Enter employee name or enter stop to quit:");
   employee=input.nextLine();    
  
  
                  
     System.out.println("Enter Employee Hourly Wage: $");      
   hourlyWage = input.nextDouble();
    
  
  
  
   System.out.println("Enter number of hours worked:");
   hoursWorked=input.nextDouble();
  
   if (hourlyWage >= 0  && hoursWorked >= 0){
   System.out.println("Employee name is " + employee);
          
   System.out.println("Employee Weekly Salary is $ " + hoursWorked * hourlyWage );
System.out.printf("\nThe employee has made: $ %.2f dollars\n\n", hoursWorked * hourlyWage);
   }  
else System.out.println("Employee hours and hourly wage must be a postive value please reenter");
   }
  
  
  
   }// end main


}//end class Payroll



I am getting no errors but when i enter stop for my employee name, it does not exit the while loop. Then when a negative number is entered, it does tell them to reenter a positive number but it does not list the name again so on the system.out.print it does not show employee name

Any help would be greatly appreciated.

Thanks

User is offlineProfile CardPM
+Quote Post


PennyBoki
RE: Payroll Program Errors
14 Sep, 2007 - 10:37 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,075



Thanked: 12 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi try to change the while condition from
while(employee != "stop"){

into

while(!employee.equals("stop")){


User is offlineProfile CardPM
+Quote Post

sswearengin
RE: Payroll Program Errors
14 Sep, 2007 - 10:41 AM
Post #3

New D.I.C Head
*

Joined: 9 Sep, 2007
Posts: 17


My Contributions
QUOTE(PennyBoki @ 14 Sep, 2007 - 11:37 AM) *

Hi try to change the while condition from
while(employee != "stop"){

into

while(!employee.equals("stop")){


That worked wonderfully for getting it to stop after the stop command was entered. It asked for employee wage and hours worked and then stopped.

But then when I enter a name like Doug, and the hourly wage and hours worked, it then prints out what is expected and goes back through the while loop and starts out by asking for the employee hourly wage, it skips the employee name

Thanks

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Payroll Program Errors
14 Sep, 2007 - 11:06 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,075



Thanked: 12 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi I've noticed that problem with the Scanner class more than once, so I go around it, by using another method for input, so here is what I did, using the BufferedReader class, and the InputStreamReader class:
CODE

// Shawn Swearengin
//IT 215
//Axia College of the University of Phoenix
//September 10, 2007

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.String;


public class Main
        
        
{
    
public static void main (String[] args)
{
          //Scanner input = new Scanner(System.in);
   BufferedReader input = new BufferedReader(new  InputStreamReader(System.in));
   String employee="___";
   double hourlyWage=0.0;
   double hoursWorked=0.0;
  

   while(!employee.equals("stop"))
   {
  
       employee=null;
       System.out.println("\n Enter employee name or enter stop to quit:");
      
       try
       {
           employee=input.readLine();
      
      
      
                      
       System.out.println("Enter Employee Hourly Wage: $");      
       hourlyWage = Double.parseDouble(input.readLine());
        
      
      
      
       System.out.println("Enter number of hours worked:");
       hoursWorked=Double.parseDouble(input.readLine());
      
       if (hourlyWage >= 0  && hoursWorked >= 0){
       System.out.println("Employee name is " + employee);
              
       System.out.println("Employee Weekly Salary is $ " + hoursWorked * hourlyWage );
        System.out.printf("\nThe employee has made: $ %.2f dollars\n\n", hoursWorked * hourlyWage);
       }  
        else System.out.println("Employee hours and hourly wage must be a postive value please reenter");
      
      
       }
        catch(Exception e){}  
   }
  
  
  
   }// end main


}//end class Payroll


If you have any questions, don't hesitate to ask smile.gif
User is offlineProfile CardPM
+Quote Post

sswearengin
RE: Payroll Program Errors
14 Sep, 2007 - 11:18 AM
Post #5

New D.I.C Head
*

Joined: 9 Sep, 2007
Posts: 17


My Contributions
QUOTE(PennyBoki @ 14 Sep, 2007 - 12:06 PM) *

Hi I've noticed that problem with the Scanner class more than once, so I go around it, by using another method for input, so here is what I did, using the BufferedReader class, and the InputStreamReader class:
CODE

// Shawn Swearengin
//IT 215
//Axia College of the University of Phoenix
//September 10, 2007

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.String;


public class Main
        
        
{
    
public static void main (String[] args)
{
          //Scanner input = new Scanner(System.in);
   BufferedReader input = new BufferedReader(new  InputStreamReader(System.in));
   String employee="___";
   double hourlyWage=0.0;
   double hoursWorked=0.0;
  

   while(!employee.equals("stop"))
   {
  
       employee=null;
       System.out.println("\n Enter employee name or enter stop to quit:");
      
       try
       {
           employee=input.readLine();
      
      
      
                      
       System.out.println("Enter Employee Hourly Wage: $");      
       hourlyWage = Double.parseDouble(input.readLine());
        
      
      
      
       System.out.println("Enter number of hours worked:");
       hoursWorked=Double.parseDouble(input.readLine());
      
       if (hourlyWage >= 0  && hoursWorked >= 0){
       System.out.println("Employee name is " + employee);
              
       System.out.println("Employee Weekly Salary is $ " + hoursWorked * hourlyWage );
        System.out.printf("\nThe employee has made: $ %.2f dollars\n\n", hoursWorked * hourlyWage);
       }  
        else System.out.println("Employee hours and hourly wage must be a postive value please reenter");
      
      
       }
        catch(Exception e){}  
   }
  
  
  
   }// end main


}//end class Payroll


If you have any questions, don't hesitate to ask smile.gif



That worked like you said, so is that clearing out the buffer or just exactly what is that catch exception e command doing. I had seen someone else that had used the buffer commands but did not know exactly what they were doing. I am really trying to grasp java but it takes a lot of patience and I just keep getting frustrated at myself. I truly appreciate the help.
THanks
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Payroll Program Errors
14 Sep, 2007 - 11:54 AM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,075



Thanked: 12 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(sswearengin @ 14 Sep, 2007 - 12:18 PM) *

That worked like you said, so is that clearing out the buffer or just exactly what is that catch exception e command doing. I had seen someone else that had used the buffer commands but did not know exactly what they were doing. I am really trying to grasp java but it takes a lot of patience and I just keep getting frustrated at myself. I truly appreciate the help.
THanks

Reading the classes is very important, I suggest when you see some class unfamiliar, just google it, find it's functions, and google some examples.
For BufferedReader check, here as wel as any other function.
So about the exception, it is needed by the compiler itself, I just catch it, and do nothing about it. The exception that needs to be caught is actually IOException, but I just simply catch it's base class which is Exception, for exceptions and their use try to look some tutorials too, they are important.
You'll need a lot of patience about Java, I know when I first started, anger, rage were my feelings when I heard about JAVA, but in time I liked it very much, this place helped me a lot, so stick around there is much to be learned here. And remember three important things:

read
read
read

wink2.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 05:42PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month