NoSuchElementException while making random access file

I'm trying to make a random access file called results.dat, accepting input from the standard input stream, redirecting input from the text file names.txt and read each record in turn, displaying its contents on the screen.

Redirection i use.

java FileResults < names.txt

names.txt

John

95

Edgar

86

Chris

77

Mark

68

Patrick

59

FileResults.java

import java.io.*;

import java.util.*;

publicclass FileResults

{

privatestaticfinallong REC_SIZE = 34;

privatestaticfinalint SURNAME_SIZE = 15;

privatestatic String surname, textMark;

privatestaticint mark;

publicstaticvoid main(String[] args)

throws IOException

{

RandomAccessFile resFile=

new RandomAccessFile("results.dat","rw");

Scanner input =new Scanner(System.in);

for (int i=1; i<6; i++)

{

System.out.println("\n\nStudent " + i +"\n");

System.out.print("Name: ");

surname = input.nextLine ();

System.out.print("Mark: ");

mark = input.nextInt();

input.nextLine();

writeString(resFile,surname,SURNAME_SIZE);

resFile.writeInt(mark);

}

System.out.println("\n\n");

long numRecords = resFile.length();

resFile.seek(0);

for (int i=0; i<numRecords; i++)

{

surname = readString(resFile, SURNAME_SIZE);

mark = resFile.readInt();

System.out.println("Name: " + surname);

System.out.println("Mark: " + mark +"\n");

}

resFile.close();

}

publicstaticvoid writeString(RandomAccessFile f,

String s,int fixedSize)throws IOException

{

int size = s.length();

if (size><=fixedSize)

{

f.writeChars(s);

for (int i=size; i<fixedSize; i++)

f.writeChar(' ');

}

else

f.writeChars(s.substring(0,fixedSize));

}

publicstatic String readString(RandomAccessFile f,

int fixedSize)throws IOException

{

String value ="";

for (int i=0; i><fixedSize; i++)

value+=f.readChar();

return value;

}

}

I get the following error.

"Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine><Unknown Source> at FileResults.main<FileResults.java:267>

Does anybody notice where i did wrong? Thanks in advance.

[4635 byte] By [Patrick.R.a] at [2007-11-27 5:05:14]
# 1

I tried this (admittedly I used the code you posted - not the version with 267+ lines!) and found it read the file OK provided there was a newline at the end of the data file.

System.out.print("Name: ");

surname = input.nextLine ();

System.out.print("Mark: ");

mark = input.nextInt();

input.nextLine(); // <-- problem at the end of the file...

pbrockway2a at 2007-7-12 10:23:39 > top of Java-index,Java Essentials,New To Java...
# 2

About your second loop:

I get Exception in thread "main" java.io.EOFException

at java.io.RandomAccessFile.readChar(Unknown Source)

at Test.readString(Test.java:65)

at Test.main(Test.java:37)

This is because you call RandomAccessFile.length():long numRecords = resFile.length();

and it gives the number of BYTES, not RECORDS:

http://java.sun.com/j2se/1.4.2/docs/api/java/io/RandomAccessFile.html#length()

You can check before each read whether file_pointer < length.

You can also catch an EOFException.

About your first loop:

You have a fixed number of iterations. It is usually better to check scanner.hasNextLine() before actually getting scanner.nextLine().

Also, maybe your names.txt lacks a finilising newline, I don't know if that can cause problems.

(edit: According to pbrockway2, it does)

tom_jansena at 2007-7-12 10:23:39 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks pbrockway2 and tom_jansen for your time. This solved the problems with both loops. Names.txt indeed lacks a finilising newline and i catch an EOFException.
Patrick.R.a at 2007-7-12 10:23:39 > top of Java-index,Java Essentials,New To Java...