[英]hasNextLine() while loop not exiting
我知道我需要使用hasNextLine()來提升行 - 但是當我對hasNextLine()進行引用時,我不確定它為什么還沒有退出while循環:
我在這里(sc)使用掃描儀來讀取輸入文件
while(sc.hasNextLine()){
//read post specifications from line
String postspecs = sc.nextLine();
//split line
String[] postspecs_s = postspecs.split(", ");
//create post
reddit.findUser(stripExtension(argfile.getName())).
addPost(postspecs_s[0], PostType.valueOf
(postspecs_s[1]), postspecs_s[2]);
System.out.println("created");
}
如果達到EOF(文件結尾)字符,'hasNextLine'將僅返回false。 停止閱讀的一種更簡單的方法是等待您從System.in讀取的標記值。
所以你會有類似的東西
String s = sc.nextLine();
while(!s.equals(SENTINEL))
//proceed and dont forget to re-read.
我認為ctrl + z會在Windows上,在iPod atm上向系統發送EOF,因此無法測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.