5

This is quite a beginner question but I'm wondering why my do...while loop is not closing.

The program is supposed to loop while the user input is not 'C', 'c', 'F', or 'f'. It seems to close when just one boolean expression in the while section is valid but not if multiple are valid.

  public class CelsToFaren
  {
      public static void main(String[] args)
      {
          // scanner setup

          Scanner sc = new Scanner(System.in);
          // Variable declarations
          int celsius;
          int answerC;
          int farenheit;
          int answerF;
          char userLetter;
          do
          {
          // initial menu options
          System.out.println("Which temperature would you like to convert from? ");
          System.out.println(" >(C)elsius ");
          System.out.println(" >(F)arenheit ");
  
          // user input of C, c, F, or f to select option
          userLetter = sc.next().charAt(0);
  
          // if user input C or c
          if ((userLetter ==  'C' || userLetter == 'c'))
          {
              System.out.print("Please enter the temperature: ");
              celsius = sc.nextInt();
              answerC = ((celsius*9/5)+32);
              System.out.println("The answer is: " + answerC + " Farenheit ");
          }
          else
          {
  
              // if user input F or f
             if ((userLetter == 'F' || userLetter == 'f'))
              {
                  System.out.print("Please enter the temperature: ");
                  farenheit = sc.nextInt();
                  answerF = ((farenheit-32)*5/9);
                  System.out.println("The answer is: " + answerF + " Celsius ");
             }
              else
              {
                  // if user input not F, f, C, or c
                 if ((userLetter != 'F' || userLetter != 'f' || userLetter != 'C' || userLetter != 'c'));
                  {
                      System.out.println("Please enter a valid option");
                  }
              }
         }
 
         } while ((userLetter != 'c') || (userLetter != 'C') || (userLetter != 'f') || (userLetter != 'F'));
  
      }
  }
Sayok Majumder
  • 1,012
  • 13
  • 28
DynoNap
  • 53
  • 4
  • As you got the answer, consider converting user input to uppercase for making the condition checking part cleaner. – digiwizkid Aug 25 '21 at 04:42

2 Answers2

5

You need to change the exit logic.

In your case 1 | 0 | 0 = true so the loop continues. You need to change it to:

while ((userLetter != 'c') && (userLetter != 'C') && (userLetter != 'f') && (userLetter != 'F'));
Alex Tbk
  • 2,042
  • 2
  • 20
  • 38
4

Your condition is wrong. Lets assume you want to break loop in if statement. It would look like

if(userLetter == 'c' || userLetter == 'C' || userLetter == 'f' || userLetter == 'F')

Now let's apply negation to get a condition under which you do not need to exit the loop

if(!(userLetter == 'c' || userLetter == 'C' || userLetter == 'f' || userLetter == 'F'))

this condition is simillar to

if(userLetter != 'c' && userLetter != 'C' && userLetter != 'f' && userLetter != 'F')
vszholobov
  • 2,133
  • 1
  • 8
  • 23