-1

I want to sequentially read each line of an input unsorted file into consecutive elements of the array until there are no more records in the file or until the input size is reached, whichever occurs first. but i can't think of a way to check the next line if its the end of the file?

This is my code:

 Scanner cin = new Scanner(System.in);
    System.out.print("Max number of items: ");
    int max = cin.nextInt();

    String[] input = new String[max];

    try {
        BufferedReader br = new BufferedReader(new FileReader("src/ioc.txt"));
        for(int i=0; i<max; i++){ //to do:check for empty record
            input[i] = br.readLine();
        }
    }
    catch (IOException e){
        System.out.print(e.getMessage());
    }

    for(int i=0; i<input.length; i++){
        System.out.println((i+1)+" "+input[i]);
    }

the file has 205 lines, if I input 210 as max, the array prints with five null elements like so..

..204 Seychelles
205 Algeria
206 null
207 null
208 null
209 null
210 null

Thanks for your responses in advance!

  • You could have wondered about how `readLine()` behaves on EOF. It returns null, so you can check that. http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine() – E_net4 Mar 19 '15 at 12:45
  • http://stackoverflow.com/questions/8270926/while-eof-in-java – Stelium Mar 19 '15 at 12:45

6 Answers6

2

From the docs:

public String readLine()

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

In other words, you should do

String aux = br.readLine();
if(aux == null)
   break;
input.add(aux)

I recomend you use a variable-size array (you can pre-allocated with the requested size if reasonable). Such that you get either the expected size or the actual number of lines, and can check later.

(depending on how long your file is, you might want to look at readAllLines() too.)

Daniel Langdon
  • 5,899
  • 4
  • 28
  • 48
1

Please refer this Number of lines in a file in Java and modify your for loop to take whatever is the least out of the entered max value or the no.of lines in the file.

Community
  • 1
  • 1
kondu
  • 410
  • 3
  • 11
1

Try :

for(int i=0; i<max; i++){ //to do:check for empty record
   if(br.readLine()!=null)
      input[i] = br.readLine();
   else
      break;
}
Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34
1

Use List<String>

List<String> lines = new ArrayList<>(); // Growing array.
try (BufferedReader br = new BufferedReader(new FileReader("src/ioc.txt"))) {
    for(;;) {
        String line = br.readLine();
        if (line == null) {
            break;
        }
        lines.add(line);
    }
} catch (IOException e) {
    System.out.print(e.getMessage());
} // Closes automatically.

// If lines wanted as array:
String[] input = lines.toArray(new String[lines.size()]);

Using a dynamically growing ArrayList is the normal way to deal with such problem.

P.S.

FileReader will read in the current platform encoding, i.e. a local file, created locally.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

You could do a null check in your first for-loop like:

public static void main(String[] args) {
    Scanner cin = new Scanner(System.in);
    System.out.print("Max number of items: ");
    int max = cin.nextInt();

    BufferedReader br = new BufferedReader(new FileReader("src/ioc.txt"));
    List<String> input = new ArrayList<>();

    String nextString;
    int i;
    for (i = 0; i < max && ((nextString = br.readline()) != null); i++) {
        input.add(nextString);
    }

    for (int j = 0; j < i; j++) {
        System.out.println((j + 1) + " " + input.get(j));
    }
}
Andbdrew
  • 11,788
  • 4
  • 33
  • 37
0
    int i=0;
    for(; i<max; i++){ //to do:check for empty record
        String line=br.readLine();
        if(line==null){
            break;
        }
        input[i] = line;
    }
    //i will contain the count of lines read. indexes 0...(i-1) represent the data.
Persixty
  • 8,165
  • 2
  • 13
  • 35
  • You're welcome. @joopEggen has moved it on a bit. I agree with him that ideally you would move over to a `List` container. My post is intended to fix your problem with minimum intervention. – Persixty Mar 19 '15 at 13:21