Hello everybody,
I know what I want to do with the code, but I am not sure where exaclty to put the "if" statements.

I am working on a weekly payroll program and I need to put in an if statement so it throws an error if the weekly hours or pay is a negative number.

Below is what i have for the code and where I thought the "if" statements needed to go, but it won't work right with them there.

Thanks in advance.
COY

package payroll_program;

// Payroll.java
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;              
   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" ) )
      if ( hoursWorkedInWeek <= 0.0 )
          System.out.println( "stop" );
       if ( hourlyWageInDollars <= 0.0 )
          System.out.println("stop" );
              {
         break; 

   }

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

      // prompt and input pay rate
      System.out.println( "Please enter pay rate: " );
     
      Double payRate = input.nextDouble();
      System.out.println();
      

      double weeklyPay = hoursWorked * payRate;

      System.out.printf( "Weekly pay is $%.2f", weeklyPay ); 

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

 } // End while

The reason they are not working is because you are putting your "if" checks before you have even read them from the console.

I tried to put them below my prompts for hours worked and pay rate but that didn't work either.
It just came up with the word stop and when I used a negative number it still calculated the pay rate it didn't tell me I had an error.

Thanks

I tried to put them below my prompts for hours worked and pay rate but that didn't work either.

Now I do not wish to be rude here but if you use common sense I guess any one can decipher that you cannot check whether a value is negative or positive BEFORE YOU HAVE EVEN READ IT from the console.

<EDIT>
Also it did not work because, closely monitor your "if" statements, all the "if" statements would be executed if and only if the user enters "stop" (for the employee name), now lets see if you can figure it out why ?

I dont want to be rude either but
I OBVIOUSLY DON"T KNOW WHERE IT NEEDS TO GO!!!!!

So can we CLUE me in as to where I need to put it?

Or do I need to keep guessing?

I have been working on it for over 2 hours and tried it in numerous places including AFTER MY PROMPTS to INPUT and still NO JOY!! I am STUMPED, that is why I asked for help!

So can we CLUE me in as to where I need to put it?

Already gave you one look more closely at your "if" statements.

And considering your tone you can forget about a ready made answer from me. May be another poster might be kind enough to do some charity work for you.

My Tone??
You were the one treating me like I was an IDIOT
I asked for help and you responded by saying nothing that I could USE to figure out what I did wrong!
Of course I knew it wasn't in the right place!
Hence the Code didn't work the way It was supposed to.

That is why I asked for help, had I known the right place to put it
I probably would not be on this forum asking for help, now would I?

I am not a CODER, so don't give me CRYPTIC answers I can't understand!

commented: Just to cool you off -1

You were the one treating me like I was an IDIOT

I guess you already proved yourself one, putting all that time and energy to writing this big post instead of concentrating on the clue I gave you.

I am not a CODER, so don't give me CRYPTIC answers I can't understand!

If you are not a coder then why do you even bother coding.

OK I have changed the placement of the If statements and tried adding a stop command in the if statement.
It still gives shows the coding is not correct and will not compile or build.
So now I am not sure what have I done incorrect?

package payroll_program;

// Payroll.java
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;              
   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" ) )
      
              {
         break; 

   }

      // prompt and input hours worked
      if ( hoursWorkedInWeek <= 0.0 ("stop"))
      else System.out.println("Please enter hours worked: " );
      Double hoursWorked = input.nextDouble();
      System.out.println();
      

      // prompt and input pay rate
      if ( hourlyWageInDollars <= 0.0 ("stop"));
        else System.out.println( "Please enter pay rate: " );
     
      Double payRate = input.nextDouble();
      System.out.println();
      

      double weeklyPay = hoursWorked * payRate;

      System.out.printf( "Weekly pay is $%.2f", weeklyPay ); 

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

 } // End while

Because I am working on an assignment for a Java class I am taking and am not sure what I am doing.

So I figured I could get some decent help on here.

Come on guys calm down!
There is more than enough shooting in the world!
Be friends!
Make love!

if ( hoursWorkedInWeek <= 0.0 )
System.out.println( "stop" );

At that moment hoursWorkedInWeek is equal to 0.0.
You told it to be 0.0 before entering the while loop.
I believe you have to write some input code o change the value.

Because I am working on an assignment for a Java class I am taking and am not sure what I am doing.

If you are not sure what you are doing, then you do not deserve to pass that class do you ???

So now I am not sure what have I done incorrect?

Let me guess you took the course which contained Java in it.

I am taking an AAIT course and this class is part of it

All I asked for was some help, which I thought was the whole pupose of this site, but you have to be a jerk about it.

I asked for was some help, which I thought was the whole pupose of this site, but you have to be a jerk about it

The only Jerk I see is you who needs to be spoon fed with a solution to all his lifes problems.

I dont want to be rude either but
I OBVIOUSLY DON"T KNOW WHERE IT NEEDS TO GO!!!!!

So can we CLUE me in as to where I need to put it?

Or do I need to keep guessing?

I have been working on it for over 2 hours and tried it in numerous places including AFTER MY PROMPTS to INPUT and still NO JOY!! I am STUMPED, that is why I asked for help!

@Coyboss:
You cannot be rude to someone you asked the help of in the first place. Yes it's obvious that you don't know where it needs to go, but stephen84s did enough to drop you hints in his posts, I don't want to judgemental here, but I suppose you should have found a way out of your program if you had picked the hints. You did not do that. Let me tell you something, you cannot be expecting spoon feeding over here, it's okay everybody needs help, but that doesn't mean that you will be served the answer on a hot plate. I do understand that you have been towards getting this working for you for long, but getting back unnecessarily at somebody who is trying to help you isn't going to help you either. He was trying to help you, when you did not concentrate on his hints now wasn't he?

>Now I do not wish to be rude here but if you use common sense I
>guess any one can decipher that you cannot check whether a value
>is negative or positive BEFORE YOU HAVE EVEN READ IT from the
>console.

That's an excessively harsh post there; I would really recommend against replying if you tend to lose your cool over silly/trivial mistakes by the OP.

> That is why I asked for help, had I known the right place to put it

Programming is much more playing dice; questions like "I don't know where to put XXX" gives off a general impression of you not putting in the effort required by the assignment.

If you find a post offensive, consider reporting it and moving on with your question rather than involving yourself in the debate and making the thread fall into the deep pits of flamewar.

> All I asked for was some help, which I thought was the whole
> pupose of this site, but you have to be a jerk about it.

That's a pretty bad attitude for someone who is asking for help and would only result in people not responding to your queries. After all, what you are asking for is free help; if you like it, take it, if not, leave it, simple.

>Now I do not wish to be rude here but if you use common sense I
>guess any one can decipher that you cannot check whether a value
>is negative or positive BEFORE YOU HAVE EVEN READ IT from the
>console.

That's an excessively harsh post there; I would really recommend against replying if you tend to lose your cool over silly/trivial mistakes by the OP.

> That is why I asked for help, had I known the right place to put it

Programming is much more playing dice; questions like "I don't know where to put XXX" gives off a general impression of you not putting in the effort required by the assignment.

If you find a post offensive, consider reporting it and moving on with your question rather than involving yourself in the debate and making the thread fall into the deep pits of flamewar.
I understand and I aplologize I was just upset over his treating me like I had not tried to do anything at all. I had spent over 2 hours on this before I asked for help. (Maybe in hindsight I should have stated this fact up front)> All I asked for was some help, which I thought was the whole
> pupose of this site, but you have to be a jerk about it.

That's a pretty bad attitude for someone who is asking for help and would only result in people not responding to your queries. After all, what you are asking for is free help; if you like it, take it, if not, leave it, simple.

I realize my mistake and it won't happen again. THanks for the info and I accept the "slap on the wrist"

@Coyboss :
You need to put the two the other two 'if' statements immediately after the reading of the two values and the break statement needs to be in the first if. So your code should take the form similar to this:

while(true){
    System.out.println("Enter employee name : ");
    // read employee name
    if(employeeName.equasIgnoreCase("stop") )
    break;
    
    //read value for hours worked
    if(hoursWorkedInWeek<=0.0){
        System.out.println("Hours worked cannot be negative/zero.");
    }
    else{
         // read value for payRate
        if(hourlyWageInDollars<=0.0){
        System.out.println("Hourly wage cannot be negative/zero.");
        }
        else{
             // calculate and show the Weekly pay
        }
    }
}

@Coyboss:
You cannot be rude to someone you asked the help of in the first place. Yes it's obvious that you don't know where it needs to go, but stephen84s did enough to drop you hints in his posts, I don't want to judgemental here, but I suppose you should have found a way out of your program if you had picked the hints. You did not do that. Let me tell you something, you cannot be expecting spoon feeding over here, it's okay everybody needs help, but that doesn't mean that you will be served the answer on a hot plate. I do understand that you have been towards getting this working for you for long, but getting back unnecessarily at somebody who is trying to help you isn't going to help you either. He was trying to help you, when you did not concentrate on his hints now wasn't he?

If you are speaking of this hint "Re: Problem inserting IF" statements #2 1 Hour Ago | Add to stephen84s's Reputation | Flag Bad Post
The reason they are not working is because you are putting your "if" checks before you have even read them from the console.
Last edited by stephen84s : 1 Hour Ago at 10:52 am. "
That wasn't a hint to me, I had already tried the if statements in numerous places, so telling me I put them before they were read by the console was "to me" slapping me in the face and saying "Your an Idiot" that is why I got upset with him.
He din't start out at all in a FRIENDLY way like somebody who wanted to help, at least that is how it seemed!
Ike

Hey Verrtuck,

I tried putting the coding where you showed me but it gives me an error saying "unreachable statement"
See code snippet below:
Ike

//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;
// read value for hourly wage
//this line here gives the unreachable statement error
         if(hourlyWageInDollars <=0.0 ){
             System.out.println("Hours worked cannot be negative/ zero." );
         }
// read vlaue for hours worked
             else {
      if(hoursWorkedInWeek<0.0){
          System.out.println("Hourly wage cannot be negative/ zero.");        }

   }

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

A statement after a break statement is unreachable.

So I guess that means I need to move the Break to under my if, else statements?

Thanks.

IKE

OK So now I got it kind of working.
I moved the "break" under my last "if" statement.

Only problem is everytime I enter the employees name it starts the program over. I have tried changing the IF for employee name to a while with no joy. I have treid to reword the statements and still no joy.

What's funny is when it builds there is no errors or syntax problems.?.!?

IKE

import java.util.Scanner; // required for keyboard input

Why is it that the only thing you input is an employeeName?
What about the rest where is that put in the programs variables?

Why is it that the only thing you input is an employeeName?
What about the rest where is that put in the programs variables?

Hey Dan,

You have me confused so I posted the entire code again maybe then you can help me see what I don't have right
Ike

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;              
   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(); 

      
                    {
         
// read value for hourly wage
         if (hoursWorkedInWeek <=0 ){
             System.out.println("Hours worked cannot be negative." );
         }
// read vlaue for hours worked
             else {
      if (hourlyWageInDollars <=0){
          System.out.println("Hourly wage cannot be negative.");        }
      else{

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

      System.out.println();
      

      // prompt and input pay rate
      System.out.println( "Please enter pay rate: " );
     Double payRate = input.nextDouble();
      System.out.println();
      

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

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

   } // End while
   }
}
employeeName = input.nextLine(); <--here input of emp

{
    // read value for hourly wage <--here hoursWorkedInWeek is not input and still=0.0       
     if (hoursWorkedInWeek <=0 ){
         System.out.println("Hours worked cannot be negative." );
     }

Have you even tried to read the advices posted here?

verruckt24's example was quite simple, not to mention it was the solution. All you needed was to replace the comments with the code to read the input (1 line)


- Read the input for the name
- use the 'if' to do the check (if stop: break)
- Read the hours worked
- Check the input (if negative break)
- Read the hourlyWageInDollars
- Check the input (if negative break)
- Since you have reached here, all the data are valid --> do the calculations


PS: I wanted to post the above algorithm but I didn't because it was what verruckt24 said and his code covered what I wanted to say completely, but it seems that we need to repeat ourselves

Have you even tried to read the advices posted here?

verruckt24's example was quite simple, not to mention it was the solution:

- Read the input for the name
- use the 'if' to do the check (if stop: break)
- Read the hours worked
- Check the input (if negative break)
- Read the hourlyWageInDollars
- Check the input (if negative break)
- Since you have reached here, all the data are valid --> do the calculations

PS: I wanted to post the above algorithm but I didn't because it was what verruckt24 said and his code covered what I wanted to say completely, but it seems that we need to repeat ourselves

I tried to use Verrukt's example and that is WHAT gave me the unreachable statement error!
I am sorry if for some reason my IDE isn't reading his coding right,
but I am trying it and have tried to move wording around and still it won't work.

Hey Dan,

When I run the program as you and others have shown me to code it, I get the following issue.

run:
*********************************Payroll Program********************************

Please enter the employee's name ('stop' to quit): bob (just a generic name I am using)Hours worked cannot be negative.
*********************************Payroll Program********************************

What could be causing this to happen?
I have went back and scanned the code to make sure I have it the same as you and others have shown me, it all seems correct.
I just don't know why its coming up with this issue.

Because first you check the hours and the you read them.

And as for the unreachable statement, you didn't copy exactly the code given to you.

This is what was given:

if (name=='stop')
  break;

-read hours

And you wrote:

if ( employeeName.equalsIgnoreCase( "stop" ) ) {
         break;
// read value for hourly wage
         if(hourlyWageInDollars <=0.0 ){
             System.out.println("Hours worked cannot be negative/ zero." );
         }

}

In case you didn't understand the difference, here a simpler version of what you wrote:

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

You put the command after the break when it should be after the break but outside the if.

Also you DIDN'T read the code given to you, you just copied it. Read again the bold comments in verruckt24's code:


if ( employeeName.equalsIgnoreCase( "stop" ) )
break;
// read value for hourly wage
if(hourlyWageInDollars <=0.0 ){
System.out.println("Hours worked cannot be negative/ zero." );
} else if ()
{
...
}

Also there I hope you know the difference between these 2 following examples:

if () {
   command_1;
   command_2;
}
if ()
    command_1;
command_2;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.