java scanner closed,Java Scanner找不到行,然后Scanner关闭错误?

I have Java code that asks for user input and then stores this data in a string variable. The below function is part of a class called 'number' and is called in the main function.

public static void setVal(int i){

Scanner readIn = new Scanner(System.in);

//while (readIn.hasNextLine()){

str = readIn.nextLine();

numCheck = false;

if (i == 1){

while (!numCheck){

if (str.contains(" ")){

System.out.println("Please input a single item.");

str = readIn.nextLine();

}

else if (!isNumeric(str)){

System.out.println("Please input a valid number.");

str = readIn.nextLine();

}

else {

numCheck = true;

value = Double.parseDouble(str);

readIn.close();

}

}

readIn.close();

}

else if (i == 2){

while (!numCheck){

if (str.contains(" ")){

System.out.println("Please input a single item.");

str = readIn.nextLine();

}

else if (!isNumeric(str)){

System.out.println("Please input a valid number.");

str = readIn.nextLine();

}

else {

numCheck = true;

secondV = Double.parseDouble(str);

readIn.close();

}

}

readIn.close();

}

else {

System.out.println("An error has occurred.");

}

// }

readIn.close();

}

Part of the main function looks like this:

number input = new number();

for (int i = 1; i <= 2; i++){

input.setVal(i);

System.out.println("Now please input a second value for computing with the first.");

input.setVal(i);

}

I use the same function twice but handing it a different argument to distinguish assignment of the input to a different variable but when it runs a second time it throws a no line found error.

Applying some other advice you can see commented out I have added a 'hasNextLine()' check to check if the line exists before executing the code but this ends up at a 'Scanner closed' error even though I invoke a new instance of Scanner every time the function runs. I have also closed the scanner appropriately to ensure minimisation of errors.

I have no idea what's going wrong as I can create a Scanner in the main function and call '.nextLine()' as many times as requried without an error but when called again through a class method, I receive these errors.

Any help is appreciated, thanks in advance.

解决方案

Scanner.close() documentation states that

If this scanner has not yet been closed then if its underlying

readable also implements the Closeable interface then the readable's

close method will be invoked. If this scanner is already closed then

invoking this method will have no effect.

On closing scanner, you are also closing System.in input stream, so when you reopen the scanner it will not find any open input stream.

Better pass scanner object from outside method as argument and then close it in calling method only when you are done with it.

Just to point out, is your String object str Static?

If not then you can't use it in your static method. Better you remove the static from method declaration.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值