[英]Stopping Scanner in the end of the line
我有一個文件,其中第一件事是一個對象的名稱,另一件事是該行的末尾-該對象的描述,我如何使用Scanner
讀取它?
SUMMARY ; 'Description summary'Description summary2'
CAR ; 'Description car'
SAVINGS ; 'description'
HOME ; 'Description home'
我已經准備好了一些類似的東西。 但是我需要在行結束時停止第二個while()
。
while(scanner.hasNext()) {
System.out.print(scanner.next());
scanner.next();
scanner.useDelimiter("'");
if(scanner.hasNext(" ")) {
scanner.next();
while(scanner.hasNext()) {
System.out.print(" = " + scanner.next());
}
}
if(scanner.hasNextLine()) {scanner.nextLine();}
scanner.useDelimiter(" ");
System.out.print("\t");
}
我這樣做的方法(可能不是最好的解決方案)是調用scanner.nextLine()
獲取整行文本(在第二個while
之前,而不是next()
),然后調用split(" ")
來將其轉換為字符串數組,然后遍歷該數組以打印它們。
嘗試
while (scanner.hasNextLine ()) {
String line = scanner.nextLine ();
String arr [] = line.split (";");
System.out.print(arr[0] + " = " + arr[1]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.