Java:从控制台读取,直到得到一个空行

时间:2011-03-31 21:53:16

标签: java

我写了这个永无止境的方法。它不打印我传递的内容,为什么?

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));
       }
    }

}

1 个答案:

答案 0 :(得分:6)

" "不是空行,而是空格。试试""

while (!(line = br.readLine()).trim().equals("")){