0

I can't figure out why the last line of my code is throwing the error "cont cannot be resolved to a variable" Basically I need to covert Fahrenheit to celsius in a loop in case they want to convert more than one temperature.

I also need to have the program ask them to re enter the temperature type if they say something other than c or f without asking them to re enter the number

Those are my two problems and heres my code thanks!

 //Jonathan Towell
//This program will convert temperatures from celsius to Fahrenheit
import javax.swing.JOptionPane;
public class FarenheitOrCelsius
{

    public static void main(String[] args) 
{
do
{
 double temp = Double.parseDouble(JOptionPane.showInputDialog("Enter the temperature to be converted"));
 String ver = JOptionPane.showInputDialog("Is that temperature in celsius or fahreneheit?\n" + "Enter C or F");

 if (ver.toLowerCase().contains("c")) //If C is entered convert to fahrenheit
 {
     double result = Math.round(((9 * (temp)/5) + 32));
     JOptionPane.showMessageDialog(null, "You entered " + temp + " degrees celsius.\n" + "That temperature in fahrenheit is " + result);
     int cont = Integer.parseInt(JOptionPane.showInputDialog("Do you want to convert another temperature?", JOptionPane.YES_NO_OPTION));
 }
 else if (ver.toLowerCase().contains("f")) //If f is entered covert to celsius
 {
     double result =  Math.round((5 *(temp - 32)/9)); //Math equation for coversion from F to C
     JOptionPane.showMessageDialog(null,  "You entered " + temp + " degrees fahrenheit.\n" + "That temperature in celsius is " + result);
     int cont = Integer.parseInt(JOptionPane.showInputDialog("Do you want to convert another temperatuer?", JOptionPane.YES_NO_OPTION));
 }
 else JOptionPane.showMessageDialog(null, "Invalid Option.\n Only enter C or F.");
 {
     System.exit(0);
 } 

} while (cont = JOptionPane.YES_OPTION);
}
}

2 Answers2

1

cont is declared in an inner scope, and so is not available to your while loop. You need to declare it before the do statement:

int cont = JOptionPane.NO_OPTION;
do {
  ...
  cont = Integer.parseInt( ...

  ...
  cont = Integer.parseInt( ...
} while (cont == JOptionPane.YES_OPTION);

Note that you also need to use double = to do the comparison in the while test.

Andy Brown
  • 18,961
  • 3
  • 52
  • 62
1

Just like a local method variable is available inside that method only, the scope of a variable declared inside a pair of curly {} braces is limited to that block of code only. A variable declared outside the block is however available in both inside as well as outside the block.

So, in the following code

do {
   int localToDoBlock = 1;
   ...
   localToDoBlock++;
} while (localToDoBlock < 10) ; // ERROR!

localToDoBlock is unavailable outside the loop including the while condition itself.

But, if you declare the counter outside the loop as

int outsideTheDoBlock = 1;
do {
       outsideTheDoBlock++;
} while (outsideTheDoBlock < 10) ; // Works now

compiler does not complain anymore because outsideTheDoBlock is available both inside and outside the loop.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89