1

i do not know if the title is correct. what i want to ask is how to stop looping if next line from method is empty. i have 2 method:

public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    rrr thisObj = new rrr();
    String command = br.readLine();
      while ((command = br.readLine())!=null) {
         int b=0;
         thisObj.perintah(b,command);
      }
}  

public void perintah(Integer indek, String PesanSumber)
   {
       String Pesan = PesanSumber.trim();
       int panjang = Pesan.length();
       if (panjang >=3)
       {
           if (Pesan.toLowerCase().trim().substring(0, 3).equals("log") )
            {
              System.out.println("bla bla bla");
            }
           else if (Pesan.toLowerCase().trim().substring(0, 4).equals("move"))
            { 
              System.out.println("bla bla bla");
            }
           else if (Pesan.toLowerCase().trim().substring(0, 4).equals("move"))
            { 
              System.out.println("bla bla bla");
            }
       }
    }

the problem is if i run it's not stop even next line is empty.

pound Pound
  • 123
  • 3
  • 12

5 Answers5

3
 String command = br.readLine();
 while ((command = br.readLine())!=null) {

First, you're missing the first line by calling readLine() before the loop. Secondly, you can add a second condition to check if the line is empty when it's not null :

 String command;
 while ((command = br.readLine())!=null && !command.isEmpty()) {

Now, it should stops where a line is empty or where there is no new line in the stream.

zakinster
  • 10,508
  • 1
  • 41
  • 52
  • 2
    From [javadocs](http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine%28%29): `BufferedReader.readLine()` return null when EOF is reached. He/she will get a NPE in this case. – Mauren Oct 09 '13 at 12:35
1

You're trying to initialize the string twice, reading an empty line, so just change

String command = br.readLine();

to

String command;
Troubleshoot
  • 1,816
  • 1
  • 12
  • 19
0

you can try following. Now it will stop.

 String command;
  while ((command = br.readLine())!=null) {
     int b=0;
     thisObj.perintah(b,command);
  }

In your code

  String command = br.readLine(); // you read empty line
  while ((command = br.readLine())!=null) { // But command will override by next line
     int b=0;
     thisObj.perintah(b,command);
  }
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

With a Scanner, you can also use .hasNext() .next() methods which is quite a classical way of doing things. Example here : BufferedReader pointer

Community
  • 1
  • 1
Julien
  • 2,544
  • 1
  • 20
  • 25
0

I was just going through your code, String command = br.readLine(); should be replaced with String command = ""; otherwise your first input will be missed. And to answer your question, System.in does not returns null, so you must check for empty string as String command;// br.readLine(); while (!(command = br.readLine()).isEmpty()) { int b = 0; thisObj.perintah(b, command); }

This should work fine.

codingenious
  • 8,385
  • 12
  • 60
  • 90