Search code examples
javabufferedreaderskip

How to ignore the rest of the line in Buffered Reader after ' ', or a specific character' on a line?


I have to ignore rest of the input line after I have read the necessary digits, using BufferedReader

So if the input is:

3 // not important 
18 //same
2 // okay

etc. I don't want to read from ' ' until the end of the line.

I have only found the method skip, but this seems to skip the whole line.


Solution

  • Try this:

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input = br.readLine().split(" ")[0];
    

    Basically this reads a line and split it into words separated by ' ' and select the first one.

    Hope this serves the purpose.