-2

I have tried two versions but both repeatedly prints out the else part of the program.

Version 1 (with line break)

try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
    while(true) {
        String input = br.readLine();
        if(input != null) {
            SomeRequest req = new SomeRequest();
            req.text = input;
            client.sendTCP(req);
        } else {
            System.out.println("non stop..."); <-- this continously prints  
        }
    }
}

Version 2 (with no new line)

try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
    while(true) {
        String input = br.readLine();
        if(input != null) {
            SomeRequest req = new SomeRequest();
            req.text = input;
            client.sendTCP(req);
        } else {
            System.out.print("non stop..."); <-- this continously prints as well?   
        }
    }
}

Version 3 (throws exception)

try(Scanner sc = new Scanner(System.in)) {
    String input = "";
    System.out.println("Enter to send");
    while(true) {
        if(!sc.hasNextLine()) {
            input = sc.nextLine();
            SomeRequest req = new SomeRequest();
            req.text = input;
            client.sendTCP(req);
        } else {
            System.out.println("not sending");
        }
    }
}

Im trying to let the user print something on the console to send it to a server as you can see. But right now the program simply prints non stop...

patriques
  • 5,057
  • 8
  • 38
  • 48
  • 1
    why even bother using a BufferedReader? – Scary Wombat Nov 30 '16 at 06:18
  • Why do you expect print and println to behave differently? I think just one version of your code is sufficient as they are pretty much the same in the context of your issue. – Koray Tugay Nov 30 '16 at 06:19
  • 1
    Possible duplicate of [for loop in Java runs 3 times before taking next input](http://stackoverflow.com/questions/23720118/for-loop-in-java-runs-3-times-before-taking-next-input) – KC Wong Nov 30 '16 at 06:19
  • 1
    Why should `client.isConnected()` become `false` on `input` becoming `null`? –  Nov 30 '16 at 06:21
  • Use System.in, make a scanner `new Scanner(System.in).nextLine();` – Ashwin Gupta Nov 30 '16 at 06:32
  • @ScaryWombat I would be delighted if you could provide an example with Scanner that works since my version is throwing exceptions. – patriques Nov 30 '16 at 06:34
  • what type of Exception? – Scary Wombat Nov 30 '16 at 06:38
  • @ScaryWombat I think he isn't including catch blocks with the try. – Ashwin Gupta Nov 30 '16 at 06:41
  • Your first and second versions only differ by the absence or presence of a line terminator in the output. This is of no relevance or interest. Your third version makes even less sense than the others. You are only sending a line if there *isn't* a line? and still no exit at end of stream. And throws *what* exception? – user207421 Nov 30 '16 at 06:50
  • @AshwinGupta How exactly do you expect me to explain other people's downvotes? – user207421 Nov 30 '16 at 06:51
  • @EJP sorry, I realized after I wrote it what that sounded like. I actually meant it as two separate things. 1) telling you I fixed it. 2) asking downvoter to explain. I just chunked it into one without thinking. Anyway, I deleted the post, yours solves the question he asked. But he says he is getting an exception, I suspect he is missing the catch blocks. – Ashwin Gupta Nov 30 '16 at 06:53
  • 1
    @AsheinGupta Don't delete your answer if you want an explanation (and certainly don't repost the same answer after deleting) – Erwin Bolwidt Nov 30 '16 at 07:02
  • 1
    @AshwinGupta You should look into try-with-resources, the OP's code is valid without a catch block and better than yours that suppresses the exception. – Erwin Bolwidt Nov 30 '16 at 07:06

1 Answers1

1

Both versions are invalid. You're not exiting the loop at end of stream. It should be:

try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
    String input;
    while ((input = br.readLine()) != null) {
        SomeRequest req = new SomeRequest();
        req.text = input;
        client.sendTCP(req);
        }
    }
    System.out.print("End of input");
}
user207421
  • 305,947
  • 44
  • 307
  • 483