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);
}
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);
}