pound Pound
pound Pound

Reputation: 123

stop looping if method read next line empty

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.

Upvotes: 1

Views: 1422

Answers (5)

codingenious
codingenious

Reputation: 8663

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.

Upvotes: 0

zakinster
zakinster

Reputation: 10698

 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.

Upvotes: 3

Troubleshoot
Troubleshoot

Reputation: 1846

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

String command = br.readLine();

to

String command;

Upvotes: 1

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35597

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

Upvotes: 0

Julien
Julien

Reputation: 2584

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

Upvotes: 0

Related Questions