2

我有一個「線程異常」main「java.lang.IllegalStateException:Scanner closed」,無法插入。Java IntToBinary應用程序:線程「main」中的異常java.lang.IllegalStateException:掃描程序關閉

我差不多完成了這個應用程序(用Java編寫)。它將int轉換爲二進制,反轉二進制,並將反轉的二進制轉換爲十進制。它運行,沒有錯誤,但是當它終止時:

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) 

任何建議,關於任何部分的應用程序,歡迎任何建議。一直在做這一點不到一年,所以我總是要學習。但主要是,我想解決上述問題。

謝謝。

<!-- language: lang-java --> 

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); 
    } 
} 

package intToBinary; 
 

 
public class AnotherOrCloseIntToBinary 
 
{ 
 
\t private static int state; 
 
\t private static String enterYOrN; 
 
\t 
 
\t public static void enterAnotherInt() 
 
\t { 
 
\t \t state = 0; 
 
\t \t while (state < 2) 
 
\t \t { 
 
\t \t \t switch (state) 
 
\t \t \t { 
 
\t \t \t \t case 0: 
 
\t \t \t \t \t System.out.println(); 
 
\t \t \t \t \t System.out.println("Do you want to enter another integer number? (y/n)"); 
 
\t \t \t \t \t break; 
 
\t \t  \t case 1: 
 
\t \t  \t \t goodBye(); 
 
\t \t  \t \t break; 
 
\t \t \t } 
 
\t \t   \t 
 
\t \t \t enterYOrN = IntToBinary.in.next(); 
 
\t \t  
 
\t \t \t if(enterYOrN.equalsIgnoreCase("y")) 
 
\t \t \t { 
 
\t \t \t \t state = 0; 
 
\t \t \t \t IntToBinary.useIntToBinary(); 
 
\t \t \t } 
 
\t \t \t else if(enterYOrN.equalsIgnoreCase("n")) 
 
\t \t \t { 
 
\t \t \t \t state++; 
 
\t \t \t } \t 
 
\t \t } 
 
\t } 
 
\t 
 
\t public static void goodBye() 
 
\t { 
 
\t  \t System.out.println("Thank you for using Int To Binary."); 
 
\t  \t System.out.println("Good bye."); 
 
\t  \t 
 
\t  \t IntToBinary.in.close(); 
 
\t } 
 
}

回答

1

您的代碼有兩個問題。一個你已經確定,這是試圖從已經關閉的Scanner讀取。您來電AnotherOrCloseIntToBinary.goodBye()作爲

 switch (state) 
    { 
     case 0: 
      System.out.println(); 
      System.out.println("Do you want to enter another integer number? (y/n)"); 
      break; 
     case 1: 
      AnotherOrCloseIntToBinary.goodBye(); 
      return; // instead of break; 
    } 

,你還沒有意識到但你while循環中調用in.nextInt()第二個問題後馬上解決它只是從enterAnotherInt()return

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(); // use in.next() here 
    } 

要查看這是如何失敗的,只需在代碼提示輸入整數時輸入非數字輸入。您將收到一個InputMismatchException。它失敗的原因是你已經在循環中,因爲輸入的不是數字,而是你的代碼繼續前進,並在掃描器上觸發一個調用,然後它將會失敗。

要解決它只是使用in.next()而不是nextInt()那裏。

+0

我剛剛測試了while,你的權利,「線程中的異常」main「java.util.InputMismatchException」。什麼是解決這個問題的好方法? – user3735449

+0

我已經添加了解決方案。也許,你只需刷新頁面來反映我的編輯。 –

+0

返回後再見()解決了我的主要問題。但是,現在「感謝您使用Int To Binary」。 「再見。」,在輸出中顯示兩次。爲了解決這個問題,可以僅僅爲了關閉和移動goodby()來做一個類,來解決這個問題? – user3735449

2

您正在嘗試從一個封閉的掃描器讀取:

  switch (state) 
     { 
      case 0: 
       System.out.println(); 
       System.out.println("Do you want to enter another integer number? (y/n)"); 
       break; 
      case 1: 
       AnotherOrCloseIntToBinary.goodBye(); // here you close the scanner 
       break; 
     } 

     enterYOrN = IntToBinary.in.next(); // here you try to read from a 
              // closed scanner 

goodBye()方法的調用也許應該只在你main的最後一行方法。

+0

糾正我,如果我錯了,但我認爲情況1與再見()&close(),只有當用戶想要退出時才能達到?狀態從0開始,用戶輸入y,移動到如果重新啓動app並將狀態返回到0的狀態。用戶輸入n,移動到else,增加狀態到1,情況1調用goodbye(),對吧? 也都說,我已經移動了關閉()周圍很多,或多或少仍然有同樣的問題。 我會更goodBye()一些。 其他想法? – user3735449

+0

非常感謝。 – user3735449

相關問題