0
public static void main(String[] args) {

    //PROBLEM: -300 pops up before program shuts down

    // Variables
    double celsiusCalculation = 0;
    String fahrenheitInput = "";

    while (!(fahrenheitInput.equals("-300"))) {

        fahrenheitInput = JOptionPane.showInputDialog(null,
                "Enter a number (in Fahrenheit) to convert to Celsius:");

        double fahrenheit = Double.parseDouble(fahrenheitInput);

        celsiusCalculation = (fahrenheit - 32) * 5 / 9;

        celsiusCalculation = (int)(celsiusCalculation * 10);
        celsiusCalculation = celsiusCalculation / 10.0;

        JOptionPane.showMessageDialog(null, celsiusCalculation);
    }
}

Hey, guys. I have a slight little problem here with my code. For some reason it calculates the Celsius conversion and gives me the answer, but when I want to input "-300" (to quit the program) it calculates -300 Fahrenheit to Celsius, and then it quits. How can I bypass this?

Thanks!

Dongbae
  • 31
  • 1
  • 8
  • Just to explain the logic, the code checks the while-loop condition before you ask for input. So the logic that is there presently is: (1) Check while-loop condition (2) Get input (3) do calculation on input (4) return to while-loop condition and see not to continue. – Ascalonian Feb 23 '15 at 03:41

2 Answers2

1

There are many ways to solve your probelm. The key is to check for "-300" immediately after you get the input from the user.

For example :

while (true) {

    fahrenheitInput = JOptionPane.showInputDialog(null,
            "Enter a number (in Fahrenheit) to convert to Celsius:");
    if (fahrenheitInput.equals("-300"))
        break;
    double fahrenheit = Double.parseDouble(fahrenheitInput);

    celsiusCalculation = (fahrenheit - 32) * 5 / 9;

    celsiusCalculation = (int)(celsiusCalculation * 10);
    celsiusCalculation = celsiusCalculation / 10.0;

    JOptionPane.showMessageDialog(null, celsiusCalculation);
}
Eran
  • 387,369
  • 54
  • 702
  • 768
1

Simply add an if to validate it

public static void main(String[] args) {

    //PROBLEM: -300 pops up before program shuts down

    // Variables
    double celsiusCalculation = 0;
    String fahrenheitInput = "";

    while (!(fahrenheitInput.equals("-300"))) {

        fahrenheitInput = JOptionPane.showInputDialog(null,
                "Enter a number (in Fahrenheit) to convert to Celsius:");
        if(!"-300".equals(fahrenheitInput))
        {
            double fahrenheit = Double.parseDouble(fahrenheitInput);

            celsiusCalculation = (fahrenheit - 32) * 5 / 9;

            celsiusCalculation = (int)(celsiusCalculation * 10);
            celsiusCalculation = celsiusCalculation / 10.0;

            JOptionPane.showMessageDialog(null, celsiusCalculation);
        }
    }
}

-300 was also calculated because even if it will exit at the end of the loop, you will still execute the rest of the code before the end.

An other option would consist in using a break :

while (!(fahrenheitInput.equals("-300"))) {

    fahrenheitInput = JOptionPane.showInputDialog(null,
            "Enter a number (in Fahrenheit) to convert to Celsius:");
    if("-300".equals(fahrenheitInput))
        break;

    double fahrenheit = Double.parseDouble(fahrenheitInput);

    celsiusCalculation = (fahrenheit - 32) * 5 / 9;

    celsiusCalculation = (int)(celsiusCalculation * 10);
    celsiusCalculation = celsiusCalculation / 10.0;

    JOptionPane.showMessageDialog(null, celsiusCalculation);
}
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
  • Thank you! This was a very specific and direct answer! All the other answers were helpful as well! Thanks everybody! – Dongbae Feb 23 '15 at 03:47