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.

