how to take the input till the end of the last line, in java???

I am using BufferedReader and want to take input on each line using readLine().split(" ")…
Now each line has two integers containing two space separated integers…and there can be about 100 such lines.

And i want to read each line and perform some operations on the integers of each line, so i want to read
each line separately until the end of the file. how can i do that in java???

Read line using br.readLine(); then store all integers in String Array. e.g String[] str= br.readLine().split(" ");
Now you can do the operation by reading String array. e.g for(int i=0;i<str.length;i++) { int a = Integer.parseInt(str[i]);
}

read the number of test cases in a integer variable for first read. From this create an array of that size to hold data. each array element will contain data for one test cases.
You need to make a counter and make a boolean variable. if counter exceeds the number of test cases read, then make the boolen true. and in while loop you need to give the boolean while(boolean_value);
as soon as all test cases are read and you make boolean true, while loop will exit.

1 Like

You can use the following code snippet.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
    while (true) {
        / code to be performed for each test case/    
    }
} catch (IOException e) {

}
1 Like

@rishy: Typically there is number of test cases in speed programming problem statement defined or another way how to stop reading input.

My favourite way, how to read input is to use BufferedReader:

BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );

number of test cases defined##

if number of test cases is defines (typically first line in input file), I’m doing…

int T = Integer.parseInt( br.readLine() )
while ( T-- > 0 ) {
    // for each test case
    String[] tmp = br.readLine().split( " " ); // typically separated by 1 space
    // in your case, there are 2 integers in line
    int i1 = Integer.parseInt( tmp[0] );
    int i2 = Integer.parseInt( tmp[1] );
    solve( i1, i2 ); // solve() method solves one test case
}

special end case##

Sometimes there is special case that simply defines end of test file, for example in your case it could be “0 0”, in such case I’d do:

String line = br.readLine();
while ( ! "0 0".equals( line ) ) {
    // for each test case, very similar to the previous code
    String[] tmp = line.split( " " );
    int i1 = Integer.parseInt( tmp[0] );
    int i2 = Integer.parseInt( tmp[1] );
    solve( i1, i2 );
    line = br.readLine(); // do not forget to read next line !
}

no special end case##

In this case you simply stop when you reach the end of file. It’s very similar to the previous one again:

String line = br.readLine();
while ( line != null ) {
    // for each test case, very similar to the previous code
    String[] tmp = line.split( " " );
    int i1 = Integer.parseInt( tmp[0] );
    int i2 = Integer.parseInt( tmp[1] );
    solve( i1, i2 );
    line = br.readLine();
}
2 Likes

you can use hasNext() method.this method to determine whether there is more data to input. when case finish to press z in windows,d in mac osx

Example:

import java.util.Scanner;
class
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);

   while(input.hasNext())
   {
      System.out.println(input.nextInt());
   }

}
}

that’s not what i asked…i just want to take the input till the last line…input file contains numerous test cases and number of test case is not known, beforehand. so basically i have the read each line till the end of the file and perform the same operations on each line…i have already used

while(r.readLine()!= null)
{
/*
code to be performed for each test case*/
}

but that doesnot seem to work, it is taking just first line of the input file.

@rishy while(r.readLine()!= null) {...} should work fine, add problem or submission link

It’s not good practice to wait for exception if you can prevent exeption throwing…

It worked…Thanks for your well written comment…:slight_smile:

I agree with you, but its just one way to use try-catch statements to our use. Also throwing exceptions is a practice I saw in codes of good java coders like EgorK and Petr.