java-使用扫描仪的NoSuchElement
作者:互联网
I have declared two strings and reading the input using Scanner(System.in).
此后,当我关闭扫描仪并再次使用扫描仪读取另一个输入时,它会引发错误:NoSuchElementException.
请指导我
import java.util.Scanner;
import java.io.*;
public class NumericInput
{
public static void main(String[] args)
{
// Declarations
Scanner in = new Scanner(System.in);
String string1;
String string2;
// Prompts
System.out.println("Enter the value of the First String .");
// Read in values
string1 = in.nextLine();
// When i am commenting below line(in.close) code is working properly.
in.close();
Scanner sc = new Scanner(System.in);
System.out.println("Now enter another value.");
string2 = sc.next();
sc.close();
System.out.println("Here is what you entered: ");
System.out.println(string1 + " and " + string2);
}
}
解决方法:
当您的扫描仪是close
时,它也会关闭System.in
输入流,您在再次使用它,但是它已关闭,因此当您再次尝试使用扫描仪时,找不到打开的System.in流.
标签:java-util-scanner,nosuchelementexception,java 来源: https://codeday.me/bug/20191028/1955317.html