0

I need to modify my program so that I can run it more than once if need be. I need to quit the program if the user enters a Q or q and if anything other than the requested entry (or the quit command) is entered the question will be repeated. Here is the code I have so far:

import java.util.Scanner;

public class TemperatureLoop
{

    private static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) 
    {

        System.out.println("Enter a temperature in degrees (for example 32.6): ");
        double temp;
        temp = keyboard.nextDouble();
        System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
        String letter = keyboard.next();
        double total = 0;
        //if Farenheit then do this equation
        if (letter.equals("F") || (letter.equals("f")))
        {
            total = ((temp-32)*5)/9; //convert the entered temperature to Celsius
            System.out.println(temp + " degrees F = " + total + " degrees Celsius");
        }
        else //if Celsius then do this
        if (letter.equals("C") || (letter.equals("c")) )
        {
            total = (((temp*9))/5)+32; //convert the entered temperature to Farenheit
            System.out.println(temp + " degrees C = " + total + " degrees Fahrenheit");
        }
    }
}
devonlazarus
  • 1,277
  • 10
  • 24
Tony
  • 11
  • 5

2 Answers2

1

I would suggest putting what you have into a while loop that breaks out if the user enters 'Q' or 'q'. Something similar to below:

// Declare your breaking condition variable outside the while loop
boolean done = false;
while (!done){
   //  Your existing code here
   //  A conditional to check for 'Q' or 'q'
   //  set done to true if the above line evaluates as true.
}
gallen
  • 1,252
  • 1
  • 18
  • 25
0

You should use a do-while loop in this case,

String letter = "";
do{
  System.out.println("Enter a temperature in degrees (for example 32.6): ");
  double temp = 0;
  while(true){
      if(keyboard.hasNextDouble())
      {
          temp = keyboard.nextDouble();
          break;
      }
      else
      {
          System.out.println("Enter a valid double");
          sc.nextLine();
      }
  }
  System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
  letter = keyboard.next();
  double total = 0;
  //if Farenheit then do this equation
  if (letter.equalsIgnoreCase("F"))
  {
      total = ((temp-32)*5)/9; //convert the entered temperature to Celsius
      System.out.println(temp + " degrees F = " + total + " degrees Celsius");
  }
  else if (letter.equalsIgnoreCase("C"))
  {   //if Celsius then do this
      total = (((temp*9))/5)+32; //convert the entered temperature to Farenheit
      System.out.println(temp + " degrees C = " + total + " degrees Fahrenheit");
  }
}while(!letter.equalsIgnoreCase("Q"));

How the loop works is, whatever is in the do part will always execute at least once. Then it will check the while condition to determine whether or not to execute the do part again. Like you said, once the user enters Q or q, the program will end because the while condition will evaluate to false and the do part will no longer be executed. Therefore, the loop will terminate in that case.

What exactly happens when you enter Q or q? The do part will technically happen, but your if-statements will be ignored since it doesn't satisfy those conditions. Once the while check is reached, the condition will evaluate to false, causing the loop to end. If you entered something like M or g, then the if-statements will be ignored but the loop won't end because the while condition won't evaluate to false so the program will ask you once again for a temperature and degrees.

Chris Gong
  • 8,031
  • 4
  • 30
  • 51
  • So with the do-while loop this will exit/terminate the program any time the user enters a "Q"? I just saw your edit, that was very helpful thank you. I had someone try to tell me that I needed to nest the entire program within "at least" three while loops in order to get it to run properly, I'm not sure why if it can work so well like this. – Tony Feb 11 '17 at 04:52
  • @Tony correct, because if the user enters `Q` or `q`, then the program will ignore the if-statements, and once the loop condition is evaluated, the loop will end because `letter` is a value that will cause the evaluation to evaluate to false – Chris Gong Feb 11 '17 at 04:54
  • For some reason my IDE (I am using Eclipse) is telling me that it cannot resolve the variable letter inside the while conditions. Is this a problem with my IDE or did I miss something? – Tony Feb 11 '17 at 05:01
  • @Tony I see, define `letter` outside of the loop. Check my edit – Chris Gong Feb 11 '17 at 05:01
  • That fixed it that problem. If the user enters a bad value for the program, what kind of code do I want to repeat the question for the user instead if getting an error and terminating the program? – Tony Feb 11 '17 at 05:04
  • `equalsIgnoreCase` would help – OneCricketeer Feb 11 '17 at 05:07
  • @cricket_007 true :) – Chris Gong Feb 11 '17 at 05:09
  • @Tony when you say bad value, could u give me an example? – Chris Gong Feb 11 '17 at 05:10
  • @ChrisGong So If I run the program and it asks the user "enter the temperature in degrees and the user enters "pie", I want the program to be able to tell the user that is incorrect and to repeat the question. and similarly for the F or C portion. – Tony Feb 11 '17 at 05:14
  • @Tony for your first question, I'd take a look at this link http://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java – Chris Gong Feb 11 '17 at 05:19
  • @Tony as for your second question, no value can break the F or C portion because you're not doing numerical calculations with the `letter` value – Chris Gong Feb 11 '17 at 05:23
  • @ChrisGong okay I understand the second half of that. I went to the link you suggested and they're throwing around a LOT of terms I'm really not familiar with like...at all, :/ I am only a first-year programmer and everything before this has been purely theory-based so...I THINK what they are saying in the accepted answer is I need to create a new method that checks the entered answer to see if it meets some kind of condition and then if it doesn't then return false and repeat the question? Sorry I new at all this, networking was so much easier for me O.o – Tony Feb 11 '17 at 05:28
  • @Tony check my edit, I added a loop within the previous loop to keep prompting the user for an input until he/she enters a valid double. If you have any questions feel free to ask – Chris Gong Feb 11 '17 at 05:56
  • @ChrisGong Thank you so much! I just want to ask so I can be sure I understand this correctly. The if statement is making sure that the user enters a double, if temp is a double, then the program breaks from the if statement and executes the next part of the program, otherwise, if temp is not a double, then the if statement prompts them to enter a valid input until a double is entered. – Tony Feb 11 '17 at 06:05
  • @ChrisGong Thanks so much for all your help! Truely – Tony Feb 11 '17 at 06:12