Scanner.hasNext如果有令牌時讀取沒有令牌時它應該結束循環,但得到錯誤沒有這樣的元素髮現異常但當我切換第1行和第2行時我工作正常。掃描儀在循環結束後讀取
public class ScannerDemo {
public static void main(String[] args) {
String s = "Hello World! 3 + 3.0 = 6.0 true ";
Long l = 13964599874l;
s = s + l;
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(s);
// find the next long token and print it
// loop for the whole scanner
while (scanner.hasNext()) {
// if the next is a long, print found and the long
// LINE 1
if (scanner.hasNextLong()) {
System.out.println("Found :" + scanner.nextLong());
}
// if no long is found, print "Not Found:" and the token
// LINE 2
System.out.println("Not Found :" + scanner.next());
}
}
}
錯誤
Not Found :Hello
Not Found :World!
Found :3
Not Found :+
Not Found :3.0
Not Found :=
Not Found :6.0
Not Found :true
Found :13964599874
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at ScannerDemo.main(ScannerDemo.java:23)
是得到了我的愚蠢的錯誤,我沒有刪除了一個問題,另外一個可以做這種無聊的事情 – JSONParser
你是什麼意思? –
我應該刪除它嗎? – JSONParser