In C++ if I wish to read input till the EOF I can do it in the following manner
while(scanf(\"%d\",&n))
{
A[i]=n;
i++;
}
I can
You can do this:
Scanner s = new Scanner(System.in);
while (s.hasNextInt()) {
A[i] = s.nextInt();
i++;
}
Scanner scanner = new Scanner(System.in);
int c = 0;
while(scanner.hasNext()){
System.out.println(++c + " " + scanner.nextLine());
}
scanner.close();
// use while instead of normal for loop.
// If you need to read a file than BufferReader is the best way to do it, as explained above.
import java.io.BufferedReader;
import java.io.FileReader;
BufferedReader br = null;
br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
}
//using Scanner class
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
// assuming that reader is an instance of java.io.BufferedReader
String line = null;
while ((line = reader.readLine()) != null) {
// do something with every line, one at a time
}
Let me know if you run into difficulties.