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?
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?
You can do this:
Scanner s = new Scanner(System.in);
while (s.hasNextInt()) {
A[i] = s.nextInt();
i++;
}
// 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.
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);
}
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.
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);
}
}
}
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 --
Hello world
I am a file
Read me until end-of-file.
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
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.
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.
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);
}
}
}
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++;
}
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++;
}
}
}