10

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?

Brian
  • 17,079
  • 6
  • 43
  • 66
zer0nes
  • 175
  • 2
  • 3
  • 10

12 Answers12

21

You can do this:

Scanner s = new Scanner(System.in);
while (s.hasNextInt()) {
    A[i] = s.nextInt();
    i++;
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • What would happen if my file contains `null` as string ??? I am unable to read full file, can this be a reason ? – Uniruddh Mar 31 '14 at 13:45
  • @I-droid - A `Scanner` processes input by dividing it into tokens based on occurrences of delimiter characters (white space by default). When you call [`Scanner#nextInt()`](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt%28%29), it expects the next token to match an integer regular expression; if it doesn't, the method will throw an exception. So having "null" in the file might very well be a problem for you, depending on how you are using the `Scanner`. – Ted Hopp Mar 31 '14 at 13:58
12
// 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.

hd1
  • 33,938
  • 5
  • 80
  • 91
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);
   }
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
3

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.

ramesh
  • 190
  • 3
  • 5
  • 14
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 {
             BufferedReader br = new BufferedReader(new FileReader("Demo.txt"));
             String line=nul;
             while( (line=br.readLine()) != null) {
                    System.out.println(line);  
             }
  }
}
Vallabh Patade
  • 4,960
  • 6
  • 31
  • 40
1

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;
}
flx
  • 14,146
  • 11
  • 55
  • 70
David
  • 54
  • 2
0

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

Md. Jamal Uddin
  • 754
  • 8
  • 17
0
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.
Roni Castro
  • 1,968
  • 21
  • 40
Himanshu
  • 13
  • 4
0

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.

-1

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);
        }
    }
}
leopal
  • 4,711
  • 1
  • 25
  • 35
-1

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++;
     } 
-1

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++;
        } 
    
      
    }
}
shubhajit22
  • 125
  • 1
  • 6