[英]Scanner not actually taking in user input
我嘗試過掃描儀的不同用途(我希望它在文件中讀取,但我也只嘗試過字符串),它只是跳過代碼,就好像它不存在一樣。 不顯示錯誤消息。
public static void main(String[] args)
throws FileNotFoundException {
Scanner test = new Scanner(System.in);
String testLine = test.next();
Scanner input = new Scanner(new File("data.txt"));
while(input.hasNextLine){
String name = input.nextLine();
String letters = input.nextLine();
System.out.println(name + ": " + letters);
}
}
您的代碼無法編譯,因為hasNextLine()
是 function,而不是 class 成員。
您實際上是在test.next();
處從System.in
讀取數據; - 您必須輸入一些文本,然后您的代碼將繼續運行。 它只是在等待用戶輸入 - 因此不會引發異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.