我写了这个永无止境的方法。它不打印我传递的内容,为什么?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
class Main {
public void readFromConsole() {
ArrayList<String> wholeInput= new ArrayList <String>();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
String line = null;
while (!(line = br.readLine()).equals(" ")){
wholeInput.add(line);
}
}
catch(IOException e){
e.printStackTrace();
}
for (int i =0; i<wholeInput.size();i++){
System.out.println(wholeInput.get(i));
}
}
}
答案 0 :(得分:6)
" "
不是空行,而是空格。试试""
while (!(line = br.readLine()).trim().equals("")){