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;
data.seek(offset);
String startname = data.readUTF();
data.seek(end - ((stringsize + datalength) * (a + 1)));
String endname = data.readUTF();
System.out.printf(startname+"\t"+"\t"+"\t"+ endname+"\n");
}
data.close();
} catch (IOException e) {
e.printStackTrace();
}
}
bigmdawgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
How are you writing the file?  Can you print that code?
Kelvin_KingCommented:
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]);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bigmdawgAuthor Commented:
I can't use any collections to store and read the data.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Kelvin_KingCommented:
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.
Kelvin_KingCommented:
If you look at the RandomAccessFile api you'll see a readLine() which returns a String.

http://java.sun.com/j2se/1.5.0/docs/api/java/io/RandomAccessFile.html

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.