0

I am sorry for the double posting but in case you still don't get it you were supposed to replace the red comments with commands on your own and not just leave them like that.
The input will not be read by itself :)

0

Hey JavaAddict,

I think I am seeing what I am doing wrong.
I need to have my "IF" statements after
my command to input the hours worked and pay rate.

IS that where I went wrong?

Thanks
IKE

0

Hey JavaAddict,

I think I am seeing what I am doing wrong.
I need to have my "IF" statements after
my command to input the hours worked and pay rate.

IS that where I went wrong?

Thanks
IKE

Isn't that logical? First you read the input and then you validate it with if-statements.
If you don't get the input what are you going to test in the 'if'

0

DUH!!! Wasn't looking right yesterday for some reason!!

OK Now I got it kind of working.
But I still have an issue, when I run the program
and I put in a negative # here is what I get now;
run:
*********************************Payroll Program********************************

Please enter the employee's name ('stop' to quit): bob
Please enter hours worked:
-98

Hours worked cannot be negative.
*********************************Payroll Program********************************

Please enter the employee's name ('stop' to quit): Please enter hours worked:
98

Hours worked cannot be negative.
*********************************Payroll Program********************************

Please enter the employee's name ('stop' to quit): Please enter hours worked:
-34

Hours worked cannot be negative.
*********************************Payroll Program********************************

Please enter the employee's name ('stop' to quit): Please enter hours worked:
34

Hours worked cannot be negative.
*********************************Payroll Program********************************

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:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 38 seconds)

I tried adding the "break" after where I enter the employee name but it just gave me the unreachabel statement problem, I know I need some sort of filter or breaking point to make it stop asking for both name and hours worked when it errors.

THank you for the help, I really appreciate it a lot.

From what I read from your earlier post I need to modify this part here to get my break :

if (employeeName.equalsIgnoreCase( "stop" ) ) {
   break;
   System.out.println();
}

I am thinking i need to add a } after my break statement to get my command in the right place is that right? something like this??

if (employeeName.equalsIgnoreCase( "stop" ) ) {
   break;}
   System.out.println();
}
0

Post your latest.
Also what are the instructions for the logic.
Do you want to break when you get any error or continue with the next loop?

0

Hey JavaAddict, Thanks for your Help!
Here is what I am trying to get it to do;
Modify the Payroll Program application so it continues to request employee information
until the user enters stop as the employee name. In addition, program the application to
check that the hourly rate and number of hours worked are positive numbers. If either the
hourly rate or the number of hours worked is not a positive value, the application should
prompt the user to enter a positive amount.

Here is my latest 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;           
   double hourlyWageInDollars = 0.0;  
   double hoursWorkedInWeek   = 0.0;  
   double weeklyWageInDollars = 0.0;  

   while ( true )

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

           //Input the  name of the employee
      System.out.printf( "Please enter the employee's  name ('stop' to quit): " );
       employeeName = input.nextLine();{
      if (employeeName.equalsIgnoreCase( "stop" ) ) {
         System.out.println();}
  
}
                          // prompt and input hours worked
       System.out.println("Please enter hours worked: " );
      Double hoursWorked = input.nextDouble();
      System.out.println();
      // read value for hours worked
      if (hoursWorkedInWeek <=0 ){
System.out.println("Hours worked cannot be negative." );
}  else
    {

      // prompt and input pay rate
      System.out.println( "Please enter pay rate: " );
     Double payRate = input.nextDouble();
      System.out.println();{
      if (hourlyWageInDollars <=0){
          System.out.println("Hourly wage cannot be negative.");
      }

      double weeklyPay = hoursWorked * payRate;
                PrintStream printf = System.out.printf("Weekly pay is $%.2f", weeklyPay);

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

   } // End while
   }
}

Now I just need to figure out how to make it do what I want it to do. I know I am close, but still a little bit off.

Thanks again.

BTW: I am using the Netbeans IDE to work on this.

IKE

0

What is the point of having these extra brackets:

employeeName = input.nextLine();
  if (employeeName.equalsIgnoreCase( "stop" ) ) {
     System.out.println();
  }
}

Also when the user enters 'stop' you want to break, so put a break int the if.
Also if the user enters negative hourlyWageInDollars he will get the message but then the weeklyPay will be displayed.
You need to add another else after the
if ( hourlyWageInDollars < = 0 ) ,
because the result should be printed if the above is not true, inside the else

Edited by pritaeas: Fixed formatting

0

Ok I Put the ELSE where you told me to but it still is giving me the following issue when I run it;
run:
*********************************Payroll Program********************************

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

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

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

Please enter the employee's name ('stop' to quit): Please enter hours worked:
0.0
Hours worked cannot be negative/zero.
*********************************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:39)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 minute 52 seconds)


Here is the code from entering the employee name to after I enter the pay rate.

//Input the  name of the employee
      System.out.printf( "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 hoursWorked = 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 payRate = input.nextDouble();
      System.out.println();{
      if (hourlyWageInDollars <=0.0){
          System.out.println("Hourly wage cannot be negative/ zero.");
      } 
      else{
     double weeklyPay = hoursWorked * payRate;
                PrintStream printf = System.out.printf("Weekly pay is $%.2f", weeklyPay);

from what you told me above I should have all my else's and if's in the right places but it still gives me issues.

What am I missing, I have tried playing with adding semicolons after the else's with no joy, I have tried adding another else after the last one and it gives me an else with no if error.
I am going bald here (lol)

Thanks IKE

1

You should go bald I guess.

Double hoursWorked = input.nextDouble();
      // read value for hours worked
      if(hoursWorkedInWeek <=0.0){
          System.out.println("Hours worked cannot be negative/zero.");

This is the height of being careless, really Coyboss, this is what exactly stephen84s was trying to tell you yesterday and I mentioned the same. Just check what variable you are reading and what variable you are comparing. And you don't stop at once you go on and do it twice.
Here it is

Double payRate = input.nextDouble();
      System.out.println();{
      if (hourlyWageInDollars <=0.0){
          System.out.println("Hourly wage cannot be negative/ zero.");
      }

You have not even taken care to change the variable names have given to you, thats how careless you have been and when somebody points that to you, you try to get back at him.

Votes + Comments
Finally someone noticed what I saw in him at the start :P
1

First of all you open a bracket where it is not needed:

Double payRate = input.nextDouble();
System.out.println();{

//Input the  name of the employee
      System.out.printf( "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 hoursWorked = input.nextDouble();
      // read value for hours worked
      if(hoursWorked <=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 payRate = input.nextDouble();
      System.out.println();
      if (payRate <=0.0){
          System.out.println("Hourly wage cannot be negative/ zero.");
      } 
      else{
     double weeklyPay = hoursWorked * payRate;
                PrintStream printf = System.out.printf("Weekly pay is $%.2f", weeklyPay);
}
}

I have changed the variables because you do this:

Double hoursWorked = input.nextDouble()
// read value for hours worked
if(hoursWorkedInWeek <=0.0) {


Also you get the error because you ask from the user to enter a number:

System.out.println("Please enter hours worked: " );
Double hoursWorked = input.nextDouble()

And you enter: 'stop'

Votes + Comments
To go this far with him, you really need should be given a much bigger positive rep.
0

You should go bald I guess.

Double hoursWorked = input.nextDouble();
      // read value for hours worked
      if(hoursWorkedInWeek <=0.0){
          System.out.println("Hours worked cannot be negative/zero.");

This is the height of being careless, really Coyboss, this is what exactly stephen84s was trying to tell you yesterday and I mentioned the same. Just check what variable you are reading and what variable you are comparing. And you don't stop at once you go on and do it twice.
Here it is

Double payRate = input.nextDouble();
      System.out.println();{
      if (hourlyWageInDollars <=0.0){
          System.out.println("Hourly wage cannot be negative/ zero.");
      }

You have not even taken care to change the variable names have given to you, thats how careless you have been and when somebody points that to you, you try to get back at him.

I am at a loss! What do you mean careless?? Haven't even changed the variable names given to me??
Those are the names of the variables I had in the original file when I first started (hourlywagesindollars & hoursworkedinweek) why would I need to change them??
Doesn't the program need them in order to run?Remember I am NOT a coder and am trying to learn, but I don't like being slapped in the face (so to speak) by someone who feels superior when I am asking for help. If I knew what I was doing I wouldn't need help.
I'm sorry if I seem dumb, but I just don't understand some of the answers or suggestions.

0

As I said in my previous post:

Double hoursWorked = input.nextDouble()
if (hoursWorkedInWeek <=0.0) {

}

You read the input and put it in: hoursWorked and then inside the if you check the variable: hoursWorkedInWeek

You don't need to be a pro-programmer to realize what you are doing and what you are writing.
You put value in: hoursWorked. Aren't you supposed to know that this is the variable you need to check. From where did the hoursWorkedInWeek come from?? It doesn't have the value you read. So why do you check it?

0

I am already a spent force with this. But I am still posting this because being a Java programmer my conscience does not allow m to leave a Java learner hanging in the middle like this. Thats also because a part of my mind tells me that you may be really lost and need some help finding your way out.
This is what I meant in my last post, but in simple, modest words.
You are doing this :

Double hoursWorked = input.nextDouble();
      // read value for hours worked
      if(hoursWorkedInWeek <=0.0){
          System.out.println("Hours worked cannot be negative/zero.");

Where you are making the mistake of assigning the input value to hoursWorked and checking the value of hoursWorkedInWeek which you have already initialized with 0. Now I guess there should be no surprises if you get a "Hours worked cannot be negative/zero".

You are doing the same thing with payrate. Assigning the input value to payrate and then checking the hourlyWageInDollars , again I don't think there should be any surprises why you get the reply.

So in essence, you should be doing this:

Double hoursWorkedInWeek = input.nextDouble();
      // read value for hours worked
      if(hoursWorkedInWeek <=0.0){
          System.out.println("Hours worked cannot be negative/zero.");

instead of

Double hoursWorked = input.nextDouble();
      // read value for hours worked
      if(hoursWorkedInWeek <=0.0){
          System.out.println("Hours worked cannot be negative/zero.");

and this

Double hourlyWageInDollars = input.nextDouble();
      System.out.println();
      if (hourlyWageInDollars <=0.0){
          System.out.println("Hourly wage cannot be negative/ zero.");
      }

instead of

Double payRate = input.nextDouble();
      System.out.println();{
      if (hourlyWageInDollars <=0.0){
          System.out.println("Hourly wage cannot be negative/ zero.");
      }

Note : I have removed the extra, unnecessary brace after the System.out.println statement.

Hope you get this. Coz this might be my last post on this thread.

EDIT : javaAddict beat me to it.

0

Ok I tried changing the hours worked and pay rate information in the code to what you told me,
now it says that hoursWorkedInWeek is already defined in Main(java.lang.string[]) and it says the same thing for the hourlyWageInDollars part also.

Do I need to remove the

double hourlyWageInDollars = 0.0;  
double hoursWorkedInWeek   = 0.0;  
double weeklyWageInDollars = 0.0;

from the coding?? So it won't give the error message.

And thanks for working with me, after your explanation to me I feel like a Dumb*** for not seeing that problem.
IKE

0

Well, you can either remove them or rename the variables - but you can't declare two different variables with the same name within the same scope.

0

Well it appears that the code is working the way it is suppposed to

YIPPEE

I want to thank everyone who was helpful in getting this working!!

Special Kudos go to Verruckt24 and JavaAddict for their OUTSTANDING SUPPORT!!

IKE

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.