title: Java IntToBinary app: Exception in thread "main" java.lang.IllegalStateException: Scanner closed
body: <p>I have an "Exception in thread "main" java.lang.IllegalStateException: Scanner closed" that I cant plug.</p>
  
  <p>I'm nearly done with this application(written in Java). It coverts an int into binary, inverts the binary, and coverts the inverted binary into decimal. It runs, no errors, but when it terminates:</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalStateException: Scanner closed
  at java.util.Scanner.ensureOpen(Unknown Source)
  at java.util.Scanner.next(Unknown Source)
  at intToBinary.AnotherOrCloseIntToBinary.enterAnotherInt(AnotherOrCloseIntToBinary.java:24)
  at intToBinary.IntToBinary.useIntToBinary(IntToBinary.java:48)
  at intToBinary.AnotherOrCloseIntToBinary.enterAnotherInt(AnotherOrCloseIntToBinary.java:29)
  at intToBinary.IntToBinary.useIntToBinary(IntToBinary.java:48)
  at intToBinary.AnotherOrCloseIntToBinary.enterAnotherInt(AnotherOrCloseIntToBinary.java:29)
  at intToBinary.IntToBinary.useIntToBinary(IntToBinary.java:48)
  at intToBinary.IntToBinary.main(IntToBinary.java:18) 
  </code></pre>
  
  <p>Any suggestions, about any part of the app, are welcome.  Been doing this a little less than a year so I'm always down to learn.  But primarily, I'd like to fix the above.</p>
  
  <p>Thanks.</p>
  
  <pre><code> &lt;!-- language: lang-java --&gt;
  
  package intToBinary;
  
  import java.util.Scanner;
  
  public class IntToBinary 
  {
  
      private static int invertedBinaryDecimal;
      private static int _n;
      private static String binaryString;
      private static String invertedBinaryString;
  
      static Scanner in = new Scanner(System.in);
  
      public static void main(String[] args) 
      {
          greeting();
          useIntToBinary();
      }
  
      public static void greeting()
      {
          System.out.println("*Int To Binary*");
      }
  
      public static void useIntToBinary()
      {
          System.out.println("Please enter an integer number: ");
          while ((!in.hasNextInt())) 
          {
              System.out.print("You did not enter an integer number.  An integer number(whole
              numbers or counting numbers) is a    number like: 1, 2, 3...");
              System.out.print("Please enter an integer number: ");
              in.nextInt();
          }
  
          _n = in.nextInt();
  
          getBinary();
          getInvertedBinary();
          getIntegerComplement();
  
          System.out.println("You entered integer number: " + _n );
          System.out.println("In binary(base 2): " + binaryString);
          System.out.println("The inversion of this binary(base 2): " + invertedBinaryString);
          System.out.println("The inverted binary(base 2) in decimal(base 10) is: " +
          invertedBinaryDecimal);
          AnotherOrCloseIntToBinary.enterAnotherInt();
      }
  
      public static void getBinary()
      {
          binaryString = Integer.toBinaryString(_n);          
      }
  
      public static void getInvertedBinary()
      {
          invertedBinaryString = binaryString.replaceAll("0", "x").replaceAll("1",
          "0").replaceAll("x", "1");
      }
  
      public static void getIntegerComplement()
      {
          invertedBinaryDecimal = Integer.parseInt(invertedBinaryString, 2);  
      }
  }
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>package intToBinary;
  
  public class AnotherOrCloseIntToBinary 
  {
   private static int state;
   private static String enterYOrN;
   
    public static void enterAnotherInt() 
    {
     state = 0;
     while (state &lt; 2) 
     {
      switch (state) 
      {
       case 0: 
        System.out.println();
        System.out.println("Do you want to enter another integer number? (y/n)");
        break;
         case 1:
          goodBye();
          break;
      }
                
      enterYOrN = IntToBinary.in.next();
        
      if(enterYOrN.equalsIgnoreCase("y")) 
      {
       state = 0;
       IntToBinary.useIntToBinary();
      }
      else if(enterYOrN.equalsIgnoreCase("n")) 
      {
       state++;
      } 
     }
    }
    
    public static void goodBye()
    {
        System.out.println("Thank you for using Int To Binary.");
        System.out.println("Good bye.");
        
        IntToBinary.in.close();
    }
  }</code></pre>
  </div>
  </div>
  </p>