title: Using Switch and Try Statement to Validate user Input
body: <p>I've been working on this for a little while and it won't compile. It gives me an error saying </p>
  
  <blockquote>
    <p>variable choice might not have been
    initialized
                  switch(choice)
                         ^</p>
  </blockquote>
  
  <p>but I have that variable set in the program. So I don't know what the problem is? Is it really something else that's keeping this from compiling? </p>
  
  <pre><code> import java.io.*;
      import javax.swing.JOptionPane;
  
      public class MyType
      {
          public static void main(String[] args)
          {
              String strChoice = "", strTryString, strTryInt, strTryDouble;
              Integer choice, tryInt;
              double tryDouble;
              boolean done = false;
  
              while(!done)
          {
                  try
                  {
                      String answer = JOptionPane.showInputDialog(null, "What's my type\n\n\n1) String\n2) integer\n3) double\n4) Quit the program");
                      choice = Integer.parseInt(strChoice);
  
                      //test for valid codes 1, 2, 3, or 4
                      if (choice&lt;1 || choice&gt;4) throw new NumberFormatException();
                      else done = true;
                  }
                      catch(NumberFormatException e)
                  {
                      JOptionPane.showInputDialog(null, "Please enter a 1, 2, 3, or 4", "Error", JOptionPane.INFORMATION_MESSAGE);
                      switch(choice)
                  {
                          case 1:
                              JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
                              break;
  
                          case 2:
                              JOptionPane.showMessageDialog(null, "Correct!");
                              tryInt = Integer.parseInt(strChoice);
                              break;
  
                          case 3:
                              JOptionPane.showMessageDialog(null, "Correct!");
                              tryDouble = Integer.parseInt(strChoice);
                              break;
  
                          case 4:
                              done = true;
                              JOptionPane.showMessageDialog(null, "Exit.");
                              System.exit(0);
                              break;
                          default:  throw new NumberFormatException();
              }
              }
  
              }
          }
      }
  </code></pre>