-5
在我的代碼方法scanner.hasNext()總是給出錯誤的結果。調試代碼時我明白了它。有人知道什麼問題?我檢查了文件。文件從字符串開始。java Scanner.hasNext始終爲假
public class TextFileImplementor {
public static void main (String [] args){
System.out.println("Enter the name of file\n");
Scanner scanner = new Scanner(System.in);
File file = new File(scanner.nextLine());
try {
scanner = new Scanner(file);
StringBuilder stringBuilder = new StringBuilder();
while (scanner.hasNext()){
stringBuilder.append(scanner.nextLine());
}
String [] strings = stringBuilder.toString().split("\\. ");
File newFile = new File("new_" + file.getName());
FileWriter fileWriter = new FileWriter(newFile, true);
for (String string :
strings) {
if (string.length()<141){
fileWriter.write(string + "\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
也許文件是空的? –
如果您打算調用'scanner.nextLine()',請使用'scanner.hasNextLine()'進行檢查。雖然可能不是問題。 –
@AndyTurner,更改爲scanner.hasNextLine()。問題沒有解決。 –