开发者

Java: read from console until getting a blank line

开发者 https://www.devze.com 2023-02-20 23:09 出处:网络
I wrote this method, which is never ending. It isn\'t printing what I\'m passing, why? import java.io.BufferedReader;

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

精彩评论

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

关注公众号