开发者

Java: read from console until getting a blank line

I wrote this method, which is never ending. It isn't printing what I'm passing, why?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

class Main {

   publi开发者_StackOverflow中文版c 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));
       }
    }

}


" " is not an empty line, it is a space. Try ""

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜