title: Use variable assigned a value inside try catch block later in application?
body: <p>So I'm trying to use my variable n after my try-catch, but I can't because my compiler is complaining that my <code>n</code> variable 'might not have been initialized'. How can I fix this? Here is the code:</p>
  
  <pre class="lang-java prettyprint-override"><code>public static void main(String[] args) {
  
      int n;
  
      boolean validInput = false;
      String scanner;
  
      while (!validInput) {
          scanner = JOptionPane.showInputDialog("Please insert an integer:");
          try {
              n = Integer.parseInt(scanner);
              validInput = true;
          } catch (NumberFormatException exception) {
              JOptionPane.showMessageDialog(null, "Not an integer!");
              validInput = false;
          }
      }
  
      System.out.println(n); //I can't use the n even though I give it a value inside my try-catch
  }
  </code></pre>