Stroring into Arrays reading from .txt file (Streamtokenizer) help...

M

michael.rygh

I want to read from a file called scores.txt this is all it contains

Kevin 10
Jack 05

I want to store them into 2 arrays (name array and score array).

Then I will compare the users score earlier in the program to the
scores in the array, then sort them and display the scores in order.

I am just having problems trying to load/read the files into the array.
Can someone look through the code and tell me what you think is wrong.
Its just causing the program to lock up therefor I have to close it.
Any help would be great! Thanks..

FileReader file = null;
//tokens in a line of the text file


try
{
System.out.println("doing high scores");
file = new FileReader("scores.txt");
String [] name1 = new String[1];
int[] nScore = new int[1];


StreamTokenizer inputStream = new StreamTokenizer(file);
inputStream.wordChars(0x20,0x7f);

int tokenType;

tokenType = inputStream.nextToken();
while (tokenType != StreamTokenizer.TT_EOF)
{

for(index9 =0; index < 2; index++)
{

name1[index9] = inputStream.sval;
inputStream.nextToken();
nScore[index9] = (int)inputStream.nval;
//inputStream.nextToken();
tokenType = inputStream.nextToken();


}

file.close();

for(int index10 = 0; index <2; index++)
{
System.out.println(name1[index10]);
System.out.println(nScore[index10]);
}



}
}
catch (Exception e)

{
if (e instanceof FileNotFoundException)
{
System.out.println("FILENAME "+"scores.txt" + " NOT FOUND");
}

if(e instanceof ArrayIndexOutOfBoundsException)
{
System.out.println("WRONG NUMBER OF FILES SPECIFIED "+" IN COMMAND
LINE");
}
System.exit(1);


}
 
B

Brandon McCombs

I want to read from a file called scores.txt this is all it contains

Kevin 10
Jack 05

I want to store them into 2 arrays (name array and score array).

Then I will compare the users score earlier in the program to the
scores in the array, then sort them and display the scores in order.

I am just having problems trying to load/read the files into the array.
Can someone look through the code and tell me what you think is wrong.
Its just causing the program to lock up therefor I have to close it.
Any help would be great! Thanks..

FileReader file = null;
//tokens in a line of the text file


try
{
System.out.println("doing high scores");
file = new FileReader("scores.txt");
String [] name1 = new String[1];
int[] nScore = new int[1];


StreamTokenizer inputStream = new StreamTokenizer(file);
inputStream.wordChars(0x20,0x7f);

int tokenType;

tokenType = inputStream.nextToken();
while (tokenType != StreamTokenizer.TT_EOF)
{

for(index9 =0; index < 2; index++)
{

name1[index9] = inputStream.sval;
inputStream.nextToken();
nScore[index9] = (int)inputStream.nval;
//inputStream.nextToken();
tokenType = inputStream.nextToken();


}

file.close();

for(int index10 = 0; index <2; index++)
{
System.out.println(name1[index10]);
System.out.println(nScore[index10]);
}



}
}
catch (Exception e)

{
if (e instanceof FileNotFoundException)
{
System.out.println("FILENAME "+"scores.txt" + " NOT FOUND");
}

if(e instanceof ArrayIndexOutOfBoundsException)
{
System.out.println("WRONG NUMBER OF FILES SPECIFIED "+" IN COMMAND
LINE");
}
System.exit(1);


}


When i had to do something like this for homework I just used the
split() method while reading one whole line at a time.

public String nflStats[][] = new String[numPlayers][numAttributes];
NFLStats() {
BufferedReader input = null;
int plyrcnt = 0;
try {
input = new BufferedReader(new FileReader("nfl.csv"));
}
catch (FileNotFoundException e) {
System.out.println("File nfl.csv not found");
}

//Parse file, split each line using a , as the field separator.
//Each token gets put into the nflStats array by player
//Fields in file assumed to be comma delimited and in the
//following order:
//number, Lname, Fname, position, years exp., school attended, team
try {
String lineFromFile = null;
while (plyrcnt < nflStats.length) {
lineFromFile = input.readLine();
String[] playerData = lineFromFile.split(",");
for (int attribute = 0; attribute <
numAttributes; attribute++) {
nflStats[plyrcnt][attribute] =
playerData[attribute];
}
plyrcnt++;
}
} catch (IOException IOex) {
System.out.println(IOex.getMessage());
}
} //end NFLStats()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top