當我嘗試運行代碼:NoSuchToken例外StringTokenizer.nextToken()
import java.io.*;
import java.util.*;
class dothis {
public static void main (String [] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("ride.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ride.out")));
StringTokenizer st = new StringTokenizer(f.readLine());
String s1 = st.nextToken();
String s2 = st.nextToken();
char[] arr = new char[6];
if (find(s1, arr, 1) == find(s2, arr, 1)) {
out.print("one");
} else {
out.println("two");
}
out.close();
}
}
與數據文件:
ABCDEF
WERTYU
它不斷輸出:
Exception_in_thread_"main"_java.util.NoSuchElementException
at_java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at_dothis.main(Unknown_Source)
我在Stack Overflow上看到了類似的問題,但在這種情況下,文本文件的第二行是空白的,因此沒有第二行肯讀。但是,此數據文件的兩行首都包含一個字符串。爲什麼一個令牌不會被讀取到第二行?