-2

Input has an unknown amount of lines of Strings of length 20 each. I am using BufferedReader to take inputs from the console. After the last input it's just waiting for the input. How can I terminate the process?

Example Input:

10100101111011111111  
00000000000000000000  
01011101110110101111   

Above 3 lines are given as input. But its not given there are going to be 3 line of input. That is unknown and my programme is just waiting for the next input after the last line, when it should be terminating.

There is no terminator specified in the problem.

Michael
  • 41,989
  • 11
  • 82
  • 128
kumarmo2
  • 1,381
  • 1
  • 20
  • 35
  • then specify one yourself – XtremeBaumer Dec 04 '17 at 08:10
  • Actually its a competitive programming challenge. The main logic i have figured out. but my programme is waiting after the last input where it should terminate . – kumarmo2 Dec 04 '17 at 08:11
  • 1
    Where does the input come from? Is it a file, System.in, or something else? – david a. Dec 04 '17 at 08:24
  • @davida. Input is being read from console. – kumarmo2 Dec 04 '17 at 08:25
  • 1
    @Manya, in that case, the termination of the input should be specified with the task. I might be your program receives an EOF at the end of the input, but it also might be something else. Also see a related question: https://stackoverflow.com/questions/16206813/how-to-terminate-scanner-when-input-is-complete – david a. Dec 04 '17 at 09:05

3 Answers3

0

This should work:

String line; 
while ((line = br.readLine()) != null) {
   // do your stuff... 
}

And once the Buffered Reader fins a null (End of the input) it will stop reading.

EDIT:

In the code above, br is the name for my BufferedReader, so in your case you should use change br to the name for your BufferedReader.

Basically the code above, does whatever you want it to do (the code you put inside the while loop) while the BufferedReader doesn't find a null value.

EDIT 2: You didn't specify that the input was taken from Console.

String line; 
while ((line = br.readLine()).length < 20) {
// do your stuff... 
}

The code will keep asking for input until you input a line with a length below 20, (Since each line has a length of 20). Essentially you can press RETURN once you are done writting the inputs. But the first code I wrote should be the proper solution for your scenario.

Alex Coronas
  • 468
  • 1
  • 6
  • 21
0

Well, you first need to logically formulate the criteria to stop executing your application.

I can suggest one: wait for X seconds -- if no input received, then terminate. If suits you, then it can be easily coded, e.g. with while (true) - break loop or with ScheduledExecutorService. Just store the last time you received the input and check (currentTime - lastTime) < X sec. in the code.

Vladimir Salin
  • 2,951
  • 2
  • 36
  • 49
  • its a competitive programming challege. It would have solved for any other scenario. but i dont think this will work here. Also the time limit is 1 sec for the problem. So I dont think I have the luxury of waiting – kumarmo2 Dec 04 '17 at 08:29
  • Then, limit it to <1 sec. Use milliseconds. Whatever. Again, think about it logically first, then ask a question on how to make it in Java. If you're struggling with logical solution, then you should ask a specific question on algorithms, giving all details of the problem and providing all details on the platform you're competing (TopCoder or any) – Vladimir Salin Dec 04 '17 at 08:34
  • I used to be in CP before if you're interested. And I know that CP pros doing their problems by theirselves. I was telling you a different thing that you should be as much specific as possible with your questions. Look at other answers -- people trying to understand what you're asking for. And finally, try to be polite next time when looking for some help. – Vladimir Salin Dec 04 '17 at 17:05
0

Below code will stop reading the input, once it encounters the "stop". You can use any string in place of "stop"

while ((strLine = br.nextLine()) != null) {

<Your code>

    if (strLine.equalsIgnoreCase("stop")) { 
        break;
    }

}
br.close();

You can try the below method, if you can't take more than three inputs from the user. This code will automatically stop taking input after reading 3 inputs:

Scanner sc = new Scanner(System.in);
String[] arr=new String[3];
for(int i=0;i<=2;i++) {
arr[i]=sc.nextLine();
}

This is another method:

while (!(strLine = sc.nextLine()).isEmpty()) { <Your code> }

But you need to press enter twice after giving the input.

Next method is,

while ((strLine = sc.nextLine()).isEmpty()) { <Your code> }

For this you need to give all your input in the same line and press enter once.

Hope this helps.

Dora
  • 191
  • 8