title: Java: Accessing string within if statement
body: <p>Having a problem displaying a string that is declared within an If statement, here is the code:</p>
  
  <pre><code>import java.util.Scanner;
  
  // This program reads a temp and prints its current state
  public class P03_01_2 {
  
      public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
  
          // get temp
          System.out.print("Enter a temperature: ");
          if (in.hasNextDouble()) {
              double temp = in.nextDouble();
  
              // determine temp type and determine state
  
              System.out.print("Enter C for Celcius or F for Fahrenheit: ");
              String type = in.nextLine();
              if (type.equals("c") || type.equals("C")) {
                  if (temp &lt;= 0) {
                      String state = "frozen";
                  } else if (temp &gt;= 100) {
                      String state = "gaseous";
                  } else {
                      String state = "water";
                  }
              }
              if (type.equals("f") || type.equals("F")) {
                  if (temp &lt;= 32) {
                      String state = "frozen";
                  } else if (temp &gt;= 212) {
                      String state = "gaseous";
                  } else {
                      String state = "water";
                  }
              } else if ((!type.equals("c") || !type.equals("C") || !type.equals("f") || !type.equals("F"))) {
                  System.out.print("Not valid input.");
                  System.exit(0);
  
              } else {
                  System.out.println("Not valid input.");
                  System.exit(0);
              }
  
              System.out.println("The state of the water is " + state);
  
          }
      }
  }
  </code></pre>
  
  <p>I have tried putting "String state = null before" the first if statement, but keep getting errors.......Thank you.</p>