Some JAVA help, please?

The_Other_One

Daemon Poster
Messages
1,321
So here's a program I'm working on for class. It reads Teams.txt(just a list of a team name, a space, then a team's wins) It's then suppose to sort by the number of wins. I got it to read the teams and scores just fine and stick them each in arrays. The problem right now is that every time I do this test, just trying to see if it'll sort at all, I get errors. Not compiler errors, just errors when I run it...

I know it's not perfect, again, this is just a test. Don't ask how I plan to sort both arrays accordingly...I'm not to that point yet :p

------

import java.io.*;
import java.util.StringTokenizer;
public class Tournament

{
public static void main (String[] args) throws IOException
{
String[] scoresA = new String[64];
String[] teamsA = new String[64];
int x = 0;
String line, name, file = "Teams.txt";
FileReader fr = new FileReader (file);
BufferedReader inFile = new BufferedReader (fr);
line = inFile.readLine();

while (line != null)
{
StringTokenizer tokens = new StringTokenizer(line);
line = inFile.readLine();
teamsA[x] = tokens.nextToken();
scoresA[x] = tokens.nextToken();
x++;
}
inFile.close();
System.out.println(scoresA[1]);
java.util.Arrays.sort(teamsA);
System.out.println(scoresA[1]);
}
}
 
I think I got it... As you can see, I just set the arrays to 64, but some of the lists only have 8 or so teams/scores. I think I have to count the lines(or perhaps tokens and divide by 2) and just set the arrays accordingly...
 
The_Other_One said:
I think I got it... As you can see, I just set the arrays to 64, but some of the lists only have 8 or so teams/scores. I think I have to count the lines(or perhaps tokens and divide by 2) and just set the arrays accordingly...

Trial and error, best way to learn! Glad to hear you got it. Nice!
 
OK, I really, really, REALLY need some help! I can't figure out how to get this damn thing to work.

A) How do I count the number of tokens IF I stick with an array?
B) How would I sort, assuming I stick with two different arrays?(both teams and score must change accordingly)
 
Would there be a way that I could say take an array of strings with something like "UNCC 17" in each string, and sort by the number at the end? I know I can just read the array and display the number using the string tokenizer, but I don't know if it's possible to use this while sorting...
 
Have you thought about making a "team" class and have it have 2 variables. one with the team name and another with the score. Then in your tournament class make an array of the "team" class and then you would be able to sort both the name and the scores while just sorting the scores.

Thats how I would go at the problem.
 
Someone suggested that but I have no clue how I would even start doing that...

Now, I once had two arrays, one with the name one with the score. The problem there is how would you go about sorting them? YOu can sort the scroes, but the teams wouldn't sort with them...
 
Back
Top Bottom