Output Problem with Payroll Program

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 5201 Views - Last Post: 30 January 2009 - 08:20 PM Rate Topic: -----

#1 coyboss   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 28-January 09

Output Problem with Payroll Program

Posted 28 January 2009 - 08:40 AM

Hi There,
I am having a problem with the output in some Java code I am doing for a class I am taking in school.
It is a payroll program that is supposed to ask for employee name, hours worked, hourly wage and compute the pay.
Also if there is a negative value input for hours worked or pay amount it is supposed to tell you not to use negavite numbers and prompt for non-negavite input, mine does this but also asks for the employee name again. Additionally it is uspposed to allow you to type stop to end the program and mine just gives an exception.

Here is my code:
package payroll_program;

// Payroll.java
import java.io.PrintStream;
import java.util.Scanner; // required for keyboard input

public class payroll
{

   // main method 
   public static void main(String args[] )
   {
   // Create a Scanner to obtain input from command window
   Scanner input = new Scanner( System.in );

   String employeeName = null;
   String cleanInputBuffer;		   
   
   while ( true )

   {
	 
	  System.out.printf( "*********************************" );
	  System.out.printf( "Payroll Program" );
	  System.out.printf( "********************************\n\n" );

//Input the  name of the employee
	  System.out.println( "Please enter the employee's  name ('stop' to quit): " );
	   employeeName = input.nextLine();
	  if (employeeName.equalsIgnoreCase( "stop" ) ) {
		 break;}

		 // prompt and input hours worked
	   System.out.println("Please enter hours worked: " );
	double  hoursWorkedInWeek = input.nextDouble();
// read value for hours worked
		if(hoursWorkedInWeek <=0.0){
	System.out.println("Hours worked cannot be negative/zero.");
	  }
	  else{
	  // prompt and input pay rate
	  System.out.println( "Please enter pay rate: " );
	 double hourlyWageInDollars = input.nextDouble();
	  System.out.println();
	  if (hourlyWageInDollars <=0.0){
		  System.out.println("Hourly wage cannot be negative/ zero.");
	  }
	  else{
	 double weeklyPay = hoursWorkedInWeek * hourlyWageInDollars;
				PrintStream printf = System.out.printf("Weekly pay is $%.2f", weeklyPay);
}
}


   } // end method main
} // end class Payroll
}



And here is the output results I get when I run the program:
run:
*********************************Payroll Program********************************

Please enter the employee's name ('stop' to quit):
bob
Please enter hours worked:
-23
Hours worked cannot be negative/zero.
*********************************Payroll Program********************************

Please enter the employee's name ('stop' to quit):
Please enter hours worked:
23
Please enter pay rate:
45

Weekly pay is $1035.00*********************************Payroll Program********************************

Please enter the employee's name ('stop' to quit):
Please enter hours worked:
stop
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextDouble(Scanner.java:2335)
at payroll_program.payroll.main(payroll.java:36)
Java Result: 1
BUILD SUCCESSFUL (total time: 23 seconds)

If anybody can help me figure out what I have wrong I would appreciate it very much. I am NOVICE at java and will need some direct instruction as to what is wrong where and how to fix it.

THanks in advance

COY


Is This A Good Question/Topic? 0
  • +

Replies To: Output Problem with Payroll Program

#2 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 08:50 AM

If you just want the program to terminate if someone enters 'stop' then use,

System.exit(0);
instead of
break;

This post has been edited by Gloin: 28 January 2009 - 08:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 09:09 AM

hello coyboss, as gloin suggested, using system.exit(0); would be better than break, In addition, to eliminate the repetition, just remove the while(true) statement which will keep looping and which is causing yuor program to stay repeating and thus if you have a problem in the break after removing the while(true) loop you can then just use the System.exit(0), i hope this helps, good luck ;)
Was This Post Helpful? 0
  • +
  • -

#4 coyboss   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 28-January 09

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 09:45 AM

View Postayman_mastermind, on 28 Jan, 2009 - 08:09 AM, said:

hello coyboss, as gloin suggested, using system.exit(0); would be better than break, In addition, to eliminate the repetition, just remove the while(true) statement which will keep looping and which is causing yuor program to stay repeating and thus if you have a problem in the break after removing the while(true) loop you can then just use the System.exit(0), i hope this helps, good luck ;)

Thanks for the Idea on the System exit info.
As for it looping, the program has to loop so it asks for other employee information until you type stop.

Part of the requirements is that it must Loop and you must type stop to end the program.

THanks

COY

UPDATE: I tried to change it to the system.exit(0) from break and I still got the same issues.

Do you want me to re-list my code so you can see it now?

COY

This post has been edited by coyboss: 28 January 2009 - 10:01 AM

Was This Post Helpful? 0
  • +
  • -

#5 markhazlett9   User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 61
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 10:22 AM

No we should be fine... What you will need to do is create a while loop what goes like this...

while(!(userInput.equals("Stop")))
{
//your code here
}

System.exit(0);



This way, when the user enters a string, you can compare it with the word "Stop". If it is not equal then it will continue looping but if it is equal to stop(with ignoring cases) then the program will break out of the loop and exit the program. Cheers
Was This Post Helpful? 0
  • +
  • -

#6 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 10:24 AM

well to acheive the looping for the name only, you will have to first of all take off the while loop that is making the whole program loop and add a do while loop for the specific part that deals with the user entering the input, for example, you could add a do while loop that loops as long as the user is entering nothing(the length of the string is 0), if the user enters a "stop" then you will add break and if the user adds anything(which is supposed to be the name) the program would get out of the do while loop and continue to the next steps, here is an example that i wrote for you:
 do{
	  System.out.println( "Please enter the employee's  name ('stop' to quit): " );
	   employeeName = input.nextLine();
	  if (employeeName.equalsIgnoreCase( "stop" ) ) {
		 break;//the program will stop,you can use system.exit(0), no prob.
		   }//end of if statement 
		 }while(employeeName.length() > 0);///the programming will keep asking the user for input as long as he enters nothing


I added some comments just to guide you and show you how it works, now you can just remove the while(true) loop and just add the above code instead just for the part that asks for name input... anyways, i hope this helps, good luck ;)

This post has been edited by ayman_mastermind: 28 January 2009 - 10:25 AM

Was This Post Helpful? 1
  • +
  • -

#7 coyboss   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 28-January 09

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 05:24 PM

View Postayman_mastermind, on 28 Jan, 2009 - 09:24 AM, said:

well to acheive the looping for the name only, you will have to first of all take off the while loop that is making the whole program loop and add a do while loop for the specific part that deals with the user entering the input, for example, you could add a do while loop that loops as long as the user is entering nothing(the length of the string is 0), if the user enters a "stop" then you will add break and if the user adds anything(which is supposed to be the name) the program would get out of the do while loop and continue to the next steps, here is an example that i wrote for you:
 do{
	  System.out.println( "Please enter the employee's  name ('stop' to quit): " );
	   employeeName = input.nextLine();
	  if (employeeName.equalsIgnoreCase( "stop" ) ) {
		 break;//the program will stop,you can use system.exit(0), no prob.
		   }//end of if statement 
		 }while(employeeName.length() > 0);///the programming will keep asking the user for input as long as he enters nothing


I added some comments just to guide you and show you how it works, now you can just remove the while(true) loop and just add the above code instead just for the part that asks for name input... anyways, i hope this helps, good luck ;)

Thanks Ayman!!

One thing though, I need the whole program to loop back to asking for the employee name after it finishes the process of calculating pay.

How can I make it do that?

I will try what you hve given me and see how that helps.

COY

View Postmarkhazlett9, on 28 Jan, 2009 - 09:22 AM, said:

while(!(userInput.equals("Stop")))
{
//your code here
}

System.exit(0);



Hey Mark,

Where in my Code would I put this at?
Just so I will get it in the right place!! :blink:
CoY

This post has been edited by coyboss: 28 January 2009 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#8 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 05:29 PM

Just exchange the one that say while(true)

You won't need the System.exit(0) if you use this method..
Was This Post Helpful? 0
  • +
  • -

#9 coyboss   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 28-January 09

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 06:03 PM

I treid what you said Gloin, but it still won't work right,
Here is my code after Adding the While code from Ayman
package payroll_program;

// Payroll.java
import java.io.PrintStream;
import java.util.Scanner; // required for keyboard input

public class payroll2 {

   // main method 
   public static void main(String args[] )
   {
   // Create a Scanner to obtain input from command window
   Scanner input = null;

   String employeeName = null;
   String cleanInputBuffer;		   
   
while(!(input.equals("Stop")))
   {

	  input = new Scanner( System.in );

	  System.out.printf( "*********************************" );
	  System.out.printf( "Payroll Program" );
	  System.out.printf( "********************************\n\n" );
  
	 
//Input the  name of the employee
	  System.out.println( "Please enter the employee's  name ('stop' to quit): " );
	   employeeName = input.nextLine();
	  if (employeeName.equalsIgnoreCase( "stop" ) ) {
				
	   
		 // prompt and input hours worked
	   System.out.println("Please enter hours worked: " );
	double  hoursWorkedInWeek = input.nextDouble();
// read value for hours worked
		if(hoursWorkedInWeek <=0.0){
	System.out.println("Hours worked cannot be negative/zero.");
	  }
	  else{
	  // prompt and input pay rate
	  System.out.println( "Please enter pay rate: " );
	 double hourlyWageInDollars = input.nextDouble();
	  System.out.println();
	  if (hourlyWageInDollars <=0.0){
		  System.out.println("Hourly wage cannot be negative/ zero.");
	  }
	  else{
	 double weeklyPay = hoursWorkedInWeek * hourlyWageInDollars;
				PrintStream printf = System.out.printf("Weekly pay is $%.2f", weeklyPay);
}
}


   } // end method main
} // end class Payroll
}
}


And here is what it gives me when I run the program, below is my Output when I run the program;
run:
*********************************Payroll Program********************************

Please enter the employee's name ('stop' to quit):
bob
Please enter hours worked:
-48
Hours worked cannot be negative/zero.
*********************************Payroll Program********************************

When I input a negative number it should only tell me I can't do it and not aske me for employees name.

Please enter the employee's name ('stop' to quit):
Please enter hours worked:
48
Please enter pay rate:
-13
Hourly wage cannot be negative/ zero.
*********************************Payroll Program********************************

Please enter the employee's name ('stop' to quit):
Please enter hours worked:
48
Please enter pay rate:
13

Weekly pay is $624.00*********************************Payroll Program********************************
Here when I input the name agian it should ask for hours worked not throw an exception.
Please enter the employee's name ('stop' to quit):
Please enter hours worked:
bob
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextDouble(Scanner.java:2335)
at payroll_program.payroll.main(payroll.java:36)
Java Result: 1
BUILD SUCCESSFUL (total time: 25 seconds)

I know that I am a pain and I am sorry if I seem stupid with this, but I just don't understand it that well.
Thanks very much for all the help!
IKE

Was This Post Helpful? 0
  • +
  • -

#10 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 06:19 PM

Don't worry, you're not a pain at all..

This problem occurs when the user inputs something that is not expected by the system, in this case you input a string and the system expects a double.

This can be solved in on of two ways,

either you use a try/catch statement
or you make the system always read a string and then parse the string into whatever type you want (although you have to make sure the string contains a value of the type you want).
I would go with the first option.
Was This Post Helpful? 0
  • +
  • -

#11 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 06:25 PM

An example of what I just said..

double  hoursWorkedInWeek = -1.0;
while (hoursWorkedInWeek <= 0) {
  try {
	hoursWorkedInWeek = input.nextDouble();
  }
  catch (Exception e) {
// do nothing
  }
  if (hoursWorkedInWeek <= 0) {
	System.out.println("Input not a positive number, please try again");
  }
}



The idea of first setting hoursWorkedInWeek to -1 is that if the input is not a number then the value will not change and the loop will re-iterate. If the input is a number although negative, the loop will still re-iterate.

Edited: code edit

This post has been edited by Gloin: 28 January 2009 - 06:28 PM

Was This Post Helpful? 1
  • +
  • -

#12 coyboss   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 28-January 09

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 08:26 PM

Thank you Gloin.

I will try it in the morning, and post an update after I try it.

COY
Was This Post Helpful? 0
  • +
  • -

#13 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Output Problem with Payroll Program

Posted 28 January 2009 - 08:52 PM

View PostGloin, on 28 Jan, 2009 - 05:25 PM, said:


I have seen, one month, KYA going with 700 replies... you are approching that record Gloin :D
Was This Post Helpful? 0
  • +
  • -

#14 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Output Problem with Payroll Program

Posted 29 January 2009 - 03:39 AM

Can't really say that I'm interested in breaking any records.
Was This Post Helpful? 0
  • +
  • -

#15 coyboss   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 28-January 09

Re: Output Problem with Payroll Program

Posted 29 January 2009 - 10:10 AM

It's Hair Pulling Time!!!!! :crazy:

OK I've tried what you showed me about the do while

Now it gives me another error, on the line
 double weeklyPay = hourlyWageInDollars * hoursWorkedInWeek;
It gives me the following error Operator * cannot be applied to double, hoursWorkedInWeek
How can I resolve this, I have tried to remove the double, but that just gives me even more errors!!

Thanks

COY
PS: Here is the new code after the changes suggested by Gloin:
package payroll_program;

// Payroll.java
import java.io.PrintStream;
import java.util.Scanner; // required for keyboard input

public class payroll2 {

   // main method 
   public static void main(String args[] )
   {
   // Create a Scanner to obtain input from command window
   Scanner input = null;

   String employeeName = null;
   String cleanInputBuffer;		   
   
while(!(input.equals("Stop")))
   {

	  input = new Scanner( System.in );

	  System.out.printf( "*********************************" );
	  System.out.printf( "Payroll Program" );
	  System.out.printf( "********************************\n\n" );
  
	 
//Input the  name of the employee
	  System.out.println( "Please enter the employee's  name ('stop' to quit): " );
	   employeeName = input.nextLine();
	  if (employeeName.equalsIgnoreCase( "stop" ) ) {
				
	   
		 // prompt and input hours worked
	   System.out.println("Please enter hours worked: " );
   double  hoursWorkedInWeek = -1.0;
while (hoursWorkedInWeek <= 0) {
  try {
	hoursWorkedInWeek = input.nextDouble();
  }
  catch (Exception e) {
// do nothing
  }
  if (hoursWorkedInWeek <= 0) {
	System.out.println("Input not a positive number, please try again");
  }
}
	  }
	 
	  // prompt and input pay rate
	  System.out.println( "Please enter pay rate: " );
		 double hourlyWageInDollars = -1.0;
	  while (hourlyWageInDollars <= 0) {
  try {
	hourlyWageInDollars = input.nextDouble();
  }
  catch (Exception e) {
// do nothing
  }
  if (hourlyWageInDollars <= 0) {
	System.out.println("Input not a positive number, please try again");
	{
	 double weeklyPay = hourlyWageInDollars * hoursWorkedInWeek;
				PrintStream printf = System.out.printf("Weekly pay is $%.2f", weeklyPay);
}
}


   } // end method main
} // end class Payroll
}
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2