這應該是真的很簡單,當,但是由於各種原因......爪哇 - 我想忽略一個空行讀取文件
String line;
String question = "";
Question qObj = new Question();
line = br.readLine(); //points to where i am in the file!
if (line == null){
System.out.println("There was no question here. ");
System.exit(1);
} else if (line.isEmpty() || line.trim().equals("") || line.trim().equals("\n")) {
// do nothing, i don't want empty lines
} else {
question = line;
}
while ((line = br.readLine())!= null){
if (line.indexOf(LoadFromDb.ANSWER_BEGIN) == 0){
dealWithAnswer(br, qObj);
qObj.setQuestion(question);
break;
} else {
if (!line.isEmpty()){
question += "\n" + line.trim();
}
}
如果第一行上面的代碼讀取只是一個空行,那麼它將空行添加到行對象,它不會跳過它。任何想法爲什麼?
你能顯示輸出? – Btuman
你爲什麼期望跳過它?將問題設置爲行並不會改變稍後讀取的行(如果您希望行在遇到while循環前先行) – MrLore
是的,您是對的MrLore。我剛剛得出同樣的結論。 ,愚蠢的錯誤,如果你想得到點數,把它作爲答案扔進去,我會標記你。 – bharal