Search code examples
javajgrasp

I am new to programming and I keep receiving an error message I don't know how to resolve


I am writing a program that receives a temperature input from the user and converts it to either Celcius or Fahrenheit but I keep receiving the error: cannot find the symbol.

I already tried changing some variable names and i've double-checked to see if it makes sense but since im new to programming I'm not really sure if I am looking at it the right way.

import javax.swing.*;



public class Lab_two {




   public static void main (String[] args) {

   Object[] options = {"Celsius",         
                       "Fahrenheit",
                       "Cancel"};

      String initialInput = JOptionPane.showInputDialog("Enter the desired temperature to convert: ");       
      System.out.println(initialInput); 

      double numToConvert = Double.parseDouble(initialInput);

      int optionDialog = JOptionPane.showOptionDialog(null, "Would you like to convert" + numToConvert + "to Celsius or Fahrenheit?",         
                                                         "Temperature Conversions", 0, JOptionPane.INFORMATION_MESSAGE,null,
                                                          options, options[2]);
      boolean lop = true;           
      while(lop) {                                                               
         switch (optionDialog) {                                                                           
            case 0: CtoF(numToConvert);
                    System.out.println(tempC);  
               break;                              
            case 1: FtoC(numToConvert);
                    System.out.println(tempF);
               break;   
            case 2: System.out.println("Program canceled");
               lop = false;                
            default: System.out.println("canceled = reset");                           
         }
      }
   }

   public static double FtoC(double tempF) {

      double tempC = 5./9. * (tempF - 32);


      return tempC;
   }

   public static double CtoF(double tempC) {
      double tempF = 1.8 * tempC + 32;

      return tempF;
   }

}

If the user enters 77 and selects the Fahrenheit conversion then I would expect the output to be 25

instead, the console displays:

Lab_two.java:28: error: cannot find symbol
                    System.out.println(tempC);  
                                       ^
  symbol:   variable tempC
  location: class Lab_two
Lab_two.java:31: error: cannot find symbol
                    System.out.println(tempF);
                                       ^

Solution

  • You are confusing the scope of your local variables in the CtoF and FtoC methods. 'tempC' and 'tempF' are only available within the scope of those methods. You need to assign the return value of the method call to a variable in the same scope as your print, or just use the method call directly in the arguments to print:

            case 0: 
                System.out.println(CtoF(numToConvert));  
                break;                              
            case 1: {
                double tmpC = FtoC(numToConvert);
                System.out.println(tmpC);
                }
                break;