By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,648 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,648 IT Pros & Developers. It's quick & easy.

Problem exiting program with if statement

P: 6
Writing a program that is to end when "stop" is entered in the name field during execution. However, I cannot seem to get it to do so. Code is below:

Expand|Select|Wrap|Line Numbers
  1.   public static void main(String[] args) {
  2.         // TODO code application logic here
  3.         Scanner scanner = new Scanner(System.in);
  4.  
  5.     String cleanInputBuffer;
  6.  
  7.         boolean end = false; // is the input name stop?
  8.         while (end == false) // as long as end is false, proceed
  9.         {
  10.         System.out.print("Enter employee name: ");
  11.         String name = scanner.nextLine();
  12.  
  13.         if (name.toLowerCase() == "stop")
  14.         {
  15.             end = true; // when the stop is detected, change the boolean
  16.  
  17.         }
  18.         System.out.print("Enter number of hours worked in a week: ");
  19.         double hours = scanner.nextDouble();
  20.  
  21.     while (hours < 0) //if a negative number will loop next command
  22.     {
  23.         System.out.print("Enter a positive number of hours worked:"); // prompt
  24.         hours = scanner.nextDouble();
  25.     }
  26.         System.out.print("Enter hourly pay rate (Do not include dollar sign): ");
  27.         double rate = scanner.nextDouble();
  28.  
  29. while (rate < 0) //if a negative number will loop next command
  30. {
  31.     System.out.print ("Enter a positive hourly rate of pay:");
  32.         rate = scanner.nextDouble();
  33. }
  34.  
  35. /* tax rates should be entered as a decimal point i.e. 20% = .20 */
  36.  
  37.         System.out.print("Enter federal tax withholding rate: ");
  38.         double fedTax = scanner.nextDouble();
  39.  
  40.         System.out.print("Enter state tax withholding rate: ");
  41.         double stateTax = scanner.nextDouble();
  42.  
  43.         System.out.println("Employee Name: " + name + ".");
  44. double grossPay;
  45.  
  46.     grossPay = rate * hours;
  47.  
  48. double fedwithHolding;
  49.  
  50.     fedwithHolding = fedTax * grossPay;
  51.  
  52. double statewithHolding;
  53.  
  54.     statewithHolding = stateTax * grossPay;
  55.  
  56. double deduction;
  57.  
  58.     deduction = fedwithHolding + statewithHolding;
  59.  
  60. double netPay;
  61.  
  62.     netPay = grossPay - deduction;
  63.  
  64. System.out.println("The netpay is $"+ netPay +".");
  65.  
  66. cleanInputBuffer = scanner.nextLine();
  67.  
  68.         } // end outer while
  69.  
  70.     } // end method main
  71.  
  72. } // end class PayrollProgramPart
  73.  
Sep 17 '12 #1

✓ answered by Rabbit

You should probably look into a java tutorial to learn the basics before taking on projects.
Expand|Select|Wrap|Line Numbers
  1. while (condition) {
  2.    if (some other condition) {
  3.       break;
  4.    }
  5. }

Share this Question
Share on Google+
7 Replies


Rabbit
Expert Mod 10K+
P: 12,421
You never bypass the rest of the code if the user enters stop. Use break to exit out of the loop.
Sep 17 '12 #2

P: 6
I'm pretty new to this. Could you give me an example on how to use the command?
Sep 17 '12 #3

Rabbit
Expert Mod 10K+
P: 12,421
You should probably look into a java tutorial to learn the basics before taking on projects.
Expand|Select|Wrap|Line Numbers
  1. while (condition) {
  2.    if (some other condition) {
  3.       break;
  4.    }
  5. }
Sep 17 '12 #4

P: 6
I was trying that but the break wasn't working. I ended up replacing the name.toLowerCase with name.contentsEquals() which allowed it to work.
Sep 17 '12 #5

Expert 5K+
P: 8,434
I'm going to take a wild guess that if you're new to Java, the toLowercase probably didn't work because you used "=" rather than "==".
Sep 18 '12 #6

P: 6
No actually. If you look at the code I posted it was "=="
Sep 27 '12 #7

Expert 5K+
P: 8,434
Whoops! Sorry, could have sworn I read a single one.
Sep 28 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.