Problem exiting program with if statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rwgwiccan
    New Member
    • Sep 2012
    • 6

    Problem exiting program with if statement

    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:

    Code:
      public static void main(String[] args) {
            // TODO code application logic here
            Scanner scanner = new Scanner(System.in);
            
        String cleanInputBuffer;
            
            boolean end = false; // is the input name stop?
            while (end == false) // as long as end is false, proceed
            {
            System.out.print("Enter employee name: ");
            String name = scanner.nextLine();
            
            if (name.toLowerCase() == "stop")
            {
                end = true; // when the stop is detected, change the boolean
          
            }
            System.out.print("Enter number of hours worked in a week: ");
            double hours = scanner.nextDouble();
            
        while (hours < 0) //if a negative number will loop next command
        {
            System.out.print("Enter a positive number of hours worked:"); // prompt
            hours = scanner.nextDouble();
        }
            System.out.print("Enter hourly pay rate (Do not include dollar sign): ");
            double rate = scanner.nextDouble();
            
    while (rate < 0) //if a negative number will loop next command
    {
        System.out.print ("Enter a positive hourly rate of pay:");
            rate = scanner.nextDouble();
    }
      
    /* tax rates should be entered as a decimal point i.e. 20% = .20 */
            
            System.out.print("Enter federal tax withholding rate: ");
            double fedTax = scanner.nextDouble();
            
            System.out.print("Enter state tax withholding rate: ");
            double stateTax = scanner.nextDouble();
            
            System.out.println("Employee Name: " + name + ".");
    double grossPay;
    
        grossPay = rate * hours;
    
    double fedwithHolding;
    
        fedwithHolding = fedTax * grossPay;
        
    double statewithHolding;
    
        statewithHolding = stateTax * grossPay;
        
    double deduction;
    
        deduction = fedwithHolding + statewithHolding;
        
    double netPay;
    
        netPay = grossPay - deduction;
        
    System.out.println("The netpay is $"+ netPay +".");
    
    cleanInputBuffer = scanner.nextLine();
        
            } // end outer while
    
        } // end method main
        
    } // end class PayrollProgramPart
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You never bypass the rest of the code if the user enters stop. Use break to exit out of the loop.

    Comment

    • rwgwiccan
      New Member
      • Sep 2012
      • 6

      #3
      I'm pretty new to this. Could you give me an example on how to use the command?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        You should probably look into a java tutorial to learn the basics before taking on projects.
        Code:
        while (condition) {
           if (some other condition) {
              break;
           }
        }

        Comment

        • rwgwiccan
          New Member
          • Sep 2012
          • 6

          #5
          I was trying that but the break wasn't working. I ended up replacing the name.toLowerCas e with name.contentsEq uals() which allowed it to work.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            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 "==".

            Comment

            • rwgwiccan
              New Member
              • Sep 2012
              • 6

              #7
              No actually. If you look at the code I posted it was "=="

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Whoops! Sorry, could have sworn I read a single one.

                Comment

                Working...