我在线程" 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
{
private static int state;
private static String enterYOrN;
public static void enterAnotherInt()
{
state = 0;
while (state < 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();
}
}
&#13;
答案 0 :(得分: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
方法的最后一行。
答案 1 :(得分:1)
您的代码存在两个问题。您已经确定的一个,它试图从已经关闭的Scanner
中读取。在致电return
之后立即enterAnotherInt()
来解决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;
}
您尚未意识到的第二个问题是in.nextInt()
循环内的while
来电。
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
。它失败的原因是你已经在循环中,因为输入不是一个数字,但是你的代码继续前进并在扫描器上发出nextInt()
调用,然后就会失败。
要解决此问题,只需使用in.next()
代替nextInt()
。