Reading integers from a sequential file.

I have to write names with 4 scores each to a sequential file and then read them back in regular order on the left and reverse order on the right. I have where the names will write back but can't get the scores with them. I tried using data.readInt() in a loop to get the scores but it won't work. Any pointers in the right direction will be appreciated. Thanks.

public void readfile() {
RandomAccessFile data = null;
File f = new File("F://fileouputtest.dat");
long end = f.length();
try {
data = new RandomAccessFile(f, "r");
for (int a = 0; a < 20; a++)
int offset = (stringsize + datalength) * a;;
String startname = data.readUTF(); - ((stringsize + datalength) * (a + 1)));
String endname = data.readUTF();
System.out.printf(startname+"\t"+"\t"+"\t"+ endname+"\n");
} catch (IOException e) {
Who is Participating?
If they way you are writting to the file is in this matter.

PersonName Score1 Score2 Score3 Score4

Then you just need to read in the whole line from the text file.

once you done that, do a split:

String Scores[] = line.split(" ");
int scoresInt[] = int[4];
and convert each of the score to int.

for(int i = 1; i < Scores.length; i ++) //start from 1 because the first string is the name of person
  scoresInt[i -1] = nteger.parseInt(Scores[i]);
How are you writing the file?  Can you print that code?
bigmdawgAuthor Commented:
I can't use any collections to store and read the data.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

It's not a collection.

I'm just saying read the data as a line.

How are you storing the data anyway ?
bigmdawgAuthor Commented:
I mean I can't use arrays,list, stacks, etc.. to read the data back. I'm writing it with a data.writeUTF() and data.writeInt() statements into a .dat file.
If you look at the RandomAccessFile api you'll see a readLine() which returns a String.

You can try and use that from read the data back from the file.

The arrays has nothing to do with reading the data, i'm only using it to store the processed data.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.