2

This is what I have so far and I have the problem written as a comment on the line I need help with. Any further suggestions would be welcome too. I just need it to end the program when the user enters "stop" as the employee name. Thanks in advance.

package payroll_program_2;
import java.util.Scanner;

        public class Main {
            public static void main(String[] args) {

            Scanner input = new Scanner( System.in );
            float hours;                                            
            float rate;                                            
            String name;
            float total_pay;


        System.out.println("Please enter employee name");         
            name = input.next();
                if (stop)               //THIS IS WHAT I NEED HELP WITH. I DO NOT KNOW HOW TO WRITE
                    end program             //IT CORRECTLY, SO I JUST TYPED WHAT I NEED IT TO DO.
                {

                }
        System.out.println("Please enter hourly rate");              
            rate = input.nextFloat();                               
            if (rate <0)                                            
                {                                                     
                    System.out.printf("Pay rate cannot be negative");   

                }
        System.out.println("Please enter hours worked");            
            hours = input.nextFloat();                                
             if (hours <0)
                {
                    System.out.printf("Hours cannot be negative");

                }
        System.out.println("Employee's total pay for this week");   
            total_pay = hours*rate;                                   

        System.out.printf("The total pay for %s is $%.2f\n", name, total_pay);        

}
1
  • 1
    When you give errors like "Hours cannot be negative" you continue on as if thing error didn't occur. Also %n is the best way to place a newline at the end in printf. Generally double is a better choice than float though it might not matter here. Commented Feb 12, 2011 at 9:04

1 Answer 1

4
if (name.equals("stop")) {  
  return;  
}
Sign up to request clarification or add additional context in comments.

4 Comments

thank you Sam. Entering that and a few more tweaks and now I'm done. :) Thank you again
don't forget to accept the answer that helped you out. click on the checkbox you see on the left. this website works on points accumulated by people answering questions well.
if ("stop".equals(name)) { return; } To Prevent Null Pointer Exception.
Good point. I don't believe it's possible for Scanner.next() to return null, though - it'll block until it gets input / throw an exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.