0

This program is supposed to add the hours from the input until the input ='s "done" but even after the input ='s "done" and the boolean is set to true in the while loop, it doesn't end the loop, and I can't figure out why. If someone inputs for example, Friday 4 done the code should output the day total and 4 as a result but it doesn't break the loop, and multiple inputs don't add the number of hours.


import java.util.Scanner;


public class SuperMarket
{
   public static void main(String args[]) throws Exception
   {
      // Declare variables.
      final String HEAD1 = "WEEKLY HOURS WORKED";
      final String DAY_FOOTER = "          Day Total ";  // Leading spaces are intentional.
      final String SENTINEL = "done";     // Named constant for sentinel value. 
      double hoursWorked = 0;             // Current record hours.
      String hoursWorkedString = "";      // String version of hours
      String hoursTotalString = "";
      String dayOfWeek;       // Current record day of week.
      double hoursTotal = 0;           // Hours total for a day.
      String prevDay = "";             // Previous day of week.
      boolean done = false;            // loop control
      Scanner input = new Scanner(System.in);
      // Print two blank lines.
      System.out.println(); 
      System.out.println(); 
      // Print heading.
      System.out.println(HEAD1);
      // Print two blank lines.
      System.out.println(); 
      System.out.println();

      // Read first record 
      System.out.println("Enter day of week or done to quit: ");
      dayOfWeek = input.nextLine();
      if(dayOfWeek.compareTo(SENTINEL) == 0)
         done = true;
      else
      {
         System.out.print("Enter hours worked: ");
         hoursWorkedString = input.nextLine();
         hoursWorked = Integer.parseInt(hoursWorkedString);
         hoursTotal= hoursTotal+hoursWorked;
         prevDay = dayOfWeek;
         System.out.println("\t" + DAY_FOOTER + String.valueOf(hoursTotal));
         
      }
      
         
      while(done == false){
          System.out.println("Enter day of week or done to quit: ");
          dayOfWeek = input.nextLine();
          if( prevDay != dayOfWeek){
              hoursTotal =0;
          }
          System.out.print("Enter hours worked: ");
          hoursWorkedString = input.nextLine();
          prevDay = dayOfWeek;
          hoursTotal= hoursTotal+hoursWorked;
          System.out.println("\t" + DAY_FOOTER + String.valueOf(hoursTotal));
          if(dayOfWeek == "done"){
              done = true;
              break;
          }
      }
   
      System.out.println(DAY_FOOTER + "(" + prevDay + ") " + hoursTotal);
               
      System.exit(0);

   } // End of main() method.
   
} // End of SuperMarket class.
  • Your condition in your while loop is ```(done == false)```, so if done is true the condition of while is not true anymore and the loop will stop. Or what do you mean? – AztecCodes Oct 14 '21 at 11:31
  • When the input for the scanner ='s "done" the boolean variable gets set to true, which should break the loop, but it doesn't. – Ryan Waldman Oct 14 '21 at 11:31
  • Check out the first comment. They are hinting that if(string1 == string2) should be changed to if(string1.equals(string2)) – Gonen I Oct 14 '21 at 11:32
  • Just a tip on mental code style `done == false` is not done, and should be coded `!done`. And `done == true` should truely be coded as `done`. – Joop Eggen Oct 14 '21 at 11:37
  • And `dayOfWeek.compareTo(SENTINEL) == 0` should be `dayOfWeek.equals(SENTINEL)`. – Andy Turner Oct 14 '21 at 11:39

1 Answers1

0

The problem is your String comparetion, you shouldn't really compare two Strings with ==, but with .equals() method.

Just change this line:

if(dayOfWeek == "done") ...

to:

if(dayOfWeek.equals("done")) ...
AP11
  • 617
  • 4
  • 11
  • This is the second most asked Java question on Stack Overflow. It really doesn't need yet another answer. Please flag such questions to close them as duplicates. See [Why are some questions marked as duplicate?](https://stackoverflow.com/help/duplicates) and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) -> Answer well-asked questions – Ivar Oct 14 '21 at 11:38