本文介紹了使用掃描儀的NoSuchElement的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
I have declared two strings and reading the input using Scanner(System.in).
之后,當我關閉Scanner并再次使用Scanner讀取另一個輸入時,它拋出錯誤: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
輸入流時,您正在再次使用它,但它已關閉,因此當您再次嘗試使用Scanner
時,找不到打開的System.in
流。
這篇關于使用掃描儀的NoSuchElement的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,