使用 Java 扫描器顺序读取标准输入时抛出异常
Exception thrown when use Java Scanner to read from stdin sequentially
我正在学习如何在 Java 中使用 Scanner,我想从 stdin 中读取一个整数和一个字符串,所以我编写了以下代码:
import java.util.Scanner;
public class TryScanner {
public void readInt() {
Scanner in = new Scanner(System.in);
System.out.println("Please type in an integer");
int length = in.nextInt();
System.out.println("Integer you type is: " + length);
in.close();
}
public void readString() {
Scanner in = new Scanner(System.in);
System.out.println("Please type in a string");
String s = in.nextLine();
System.out.println("String you type is: " + s);
in.close();
}
public static void main(String[] args) {
TryScanner instance = new TryScanner();
instance.readInt();
// throw exception when called after readInt()
instance.readString();
}
}
我的 readInt
和 readString
函数分别运行良好,但是,当我顺序调用它们时,只有第一个函数运行,第二个函数抛出异常:
Please type in an integer
3
Integer you type is: 3
Please type in a string
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1597)
at helloworld.Sort.readString(Sort.java:17)
at helloworld.Sort.main(Sort.java:27)
谁能解释一下我哪里做错了?
删除对
的调用
in.close();
当您关闭 in
时,您还 close
System.in
全局(并且它不会重新打开)。
public void readInt() {
Scanner in = new Scanner(System.in);
System.out.println("Please type in an integer");
int length = in.nextInt();
System.out.println("Integer you type is: " + length);
// in.close();
}
public void readString() {
Scanner in = new Scanner(System.in);
System.out.println("Please type in a string");
String s = in.nextLine();
System.out.println("String you type is: " + s);
// in.close();
}
此外,您可以考虑将 Scanner
传递给方法。
public static int readInt(Scanner in) {
System.out.println("Please type in an integer");
return in.nextInt();
}
public static String readString(Scanner in) {
System.out.println("Please type in a string");
return in.nextLine();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Integer you type is: " + readInt(in));
System.out.println("String you type is: " + readString(in));
}
您也可以使用下面的代码
import java.util.Scanner;
public class TryScanner {
Scanner in = new Scanner(System.in);
public void readInt() {
System.out.println("Please type in an integer");
int length = in.nextInt();
System.out.println("Integer you type is: " + length);
}
public void readString() {
System.out.println("Please type in a string");
String s = in.nextLine();
System.out.println("String you type is: " + s);
}
public static void main(String[] args) {
TryScanner instance = new TryScanner();
instance.readInt();
instance.readString();
instance.in.close();
}
}
我正在学习如何在 Java 中使用 Scanner,我想从 stdin 中读取一个整数和一个字符串,所以我编写了以下代码:
import java.util.Scanner;
public class TryScanner {
public void readInt() {
Scanner in = new Scanner(System.in);
System.out.println("Please type in an integer");
int length = in.nextInt();
System.out.println("Integer you type is: " + length);
in.close();
}
public void readString() {
Scanner in = new Scanner(System.in);
System.out.println("Please type in a string");
String s = in.nextLine();
System.out.println("String you type is: " + s);
in.close();
}
public static void main(String[] args) {
TryScanner instance = new TryScanner();
instance.readInt();
// throw exception when called after readInt()
instance.readString();
}
}
我的 readInt
和 readString
函数分别运行良好,但是,当我顺序调用它们时,只有第一个函数运行,第二个函数抛出异常:
Please type in an integer
3
Integer you type is: 3
Please type in a string
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1597)
at helloworld.Sort.readString(Sort.java:17)
at helloworld.Sort.main(Sort.java:27)
谁能解释一下我哪里做错了?
删除对
的调用in.close();
当您关闭 in
时,您还 close
System.in
全局(并且它不会重新打开)。
public void readInt() {
Scanner in = new Scanner(System.in);
System.out.println("Please type in an integer");
int length = in.nextInt();
System.out.println("Integer you type is: " + length);
// in.close();
}
public void readString() {
Scanner in = new Scanner(System.in);
System.out.println("Please type in a string");
String s = in.nextLine();
System.out.println("String you type is: " + s);
// in.close();
}
此外,您可以考虑将 Scanner
传递给方法。
public static int readInt(Scanner in) {
System.out.println("Please type in an integer");
return in.nextInt();
}
public static String readString(Scanner in) {
System.out.println("Please type in a string");
return in.nextLine();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Integer you type is: " + readInt(in));
System.out.println("String you type is: " + readString(in));
}
您也可以使用下面的代码
import java.util.Scanner;
public class TryScanner {
Scanner in = new Scanner(System.in);
public void readInt() {
System.out.println("Please type in an integer");
int length = in.nextInt();
System.out.println("Integer you type is: " + length);
}
public void readString() {
System.out.println("Please type in a string");
String s = in.nextLine();
System.out.println("String you type is: " + s);
}
public static void main(String[] args) {
TryScanner instance = new TryScanner();
instance.readInt();
instance.readString();
instance.in.close();
}
}