'reading input till EOF in java
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 then run this code as ./a.out < input.txt. What is the java equivalent of this code?
Solution 1:[1]
// 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.
Solution 2:[2]
Here is Java equivalent code using BufferedReader and FileReader classes.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class SmallFileReader {
public static void main(String[] args) throws IOException {
Option 1:String fileName = args[0];BufferedReader br = new BufferedReader(new FileReader(fileName));
Option 2:BufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.println("Enter a file name: ");String fileName = br.readLine();
//BufferedReader br = new BufferedReader(new FileReader("Demo.txt"));
String line=null;
while( (line=br.readLine()) != null) {
System.out.println(line);
}
}
}
I made little modification to @Vallabh Code.
@tom You can use the first option, if you want to input the file name through command line.java SmallFileReader Hello.txt
Option 2 will ask you the file name when you run the file.
Solution 3:[3]
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);
}
Solution 4:[4]
Here is Java equivalent code using BufferedReader and FileReader classes.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class SmallFileReader {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("Demo.txt"));
String line=nul;
while( (line=br.readLine()) != null) {
System.out.println(line);
}
}
}
Solution 5:[5]
The only thing that really works for me (you don't even have to create a file)
Scanner read = new Scanner(System.in);
String cadena;
boolean cond = true;
int i =0;
while (cond){
cadena = read.nextLine();
if(cadena.isEmpty())
cond = false;
}
Solution 6:[6]
Here is my Java equivalent code to read input until End-of-File:
import java.util.Scanner;
public class EndOfFileSolutions {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for(int i = 1; sc.hasNext()== true; i++){
System.out.println(i + " " + sc.nextLine());
}
}
}
This code will produce output look like below --
Sample Input:
Hello world
I am a file
Read me until end-of-file.
Sample Output:
1 Hello world
2 I am a file
3 Read me until end-of-file.
This answer also works fine to solve Hackerrank EOF problem
Solution 7:[7]
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.
Solution 8:[8]
The eof() function is used to check if the End Of File (EOF) is reached. It returns 1 if EOF is reached or if the FileHandle is not open and undef in all other cases.
Solution 9:[9]
A simple solution would be to use Scanner class.
See snipet below:
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNextLine())
{
String line = s.nextLine();
System.out.println(line);
}
}
}
Solution 10:[10]
In Java, One way of reading till EOF is:-
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()) //read until condition is true
{
String line = scan.nextLine();
System.out.println(line);
i++;
}
Solution 11:[11]
EOF Or the End of the line..! Can be achieved in the single while Loop condition..!!, until the condition is valid the loop runs and taking the input from the user.".hasNextLine", is checking if there is another input line. For example, I am using Hackerrank practice. Hope This is helpful to you.
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
int i=1;
Scanner sc = new Scanner(System.in); // Making Scanner object
while(sc.hasNextLine()){ // This is the main condition for EOF
String line = sc.nextLine();
System.out.format("%d %s\n",i,line);
i++;
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
