Java: read from the console until you get an empty string

I wrote this method that never ends. This is not a seal of what I transmit, why?

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

}

+3
source share
1 answer

" "- not an empty string, this is a space. Try""

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

All Articles