Brendan O'Leary
Brendan O'Leary

Reputation: 11

Store text file info to an array

I am trying to show the results of the games played so far and I have 3 text files (results, fixtures and teamsOrPlayers). I want to be able to read the teamNames into an array and then be able to portray the results like (Arsenal 2:1 Man City)

 1:    import java.io.*;
 2:    import java.util.*;
 3:    import javax.swing.JOptionPane;
 4:
 5:    public class Text3
 6:    {
 7:        public static void main(String args[])
 8:        {
 9:
10:           // Declaring the text files
11:           File results = new File ("PremiershipResults.txt");
12:           File fixtures = new File ("PremiershipFixtures.txt");
13:           File teamsOrPlayers = new File("PremiershipTeamsOrPlayers.txt");
14:
15:           String lineFromFile ;
16:
17:           //Decalring 2 arrays to store the fixtures and results in
18:                 int fixturesArray [] ;
19:           int resultsArray [] ;
20:
21:                 //Im not sure whether these are needed just something i found on the                             internet
22:                 int count = 0 , teamsCount = 0 , teamNumber;


23:         //This is stating how many teams there are and adding 1 to count everytime there is a team

24:                 Scanner input = new Scanner (teamOrPlayers)
25:         while (input.hasNext())
26:         {
27:             input.nextLine();
28:             count++;
29:         }
30:         input.close();
31:
32:         String teamNames [] = new String [count] ;
33:         Scanner input = new Scanner (teamsOrPlayers);
34:         while (input.hasNext())
35:          {
36:             lineFromFile = input.nextLine();
37:             //The text files are seperated by commas eg. Results file would be as follows - 1,2,3 and this means fixture 1 and the result is 2-3
38:
39:             teamsArray = lineFromFile.split(",") ;
40:
41:
42:
43:         //This is the code i got off a friend and he said it would work if i can store the info into arrays

44:
45:
46:             for(int i = 0; i < results.get(0).size(); i++)
47:             {
48:              int homeTeam = Integer.parseInt(fixtures.get(1).get(i));
49:              int awayTeam = Integer.parseInt(fixtures.get(2).get(i));
50:              String homeTeamStr = teamsOrPlayers.get(1).get(homeTeam - 1);
51:              String awayTeamStr = teamsOrPlayers.get(1).get(awayTeam - 1);
52:
53:              int homeResult = Integer.parseInt(results.get(1).get(i));
54:              int awayResult = Integer.parseInt(results.get(2).get(i));
55:
56:              System.out.printf("%s %s - %s %s\n", homeTeamStr, homeResult, awayResult,     awayTeamStr);
57:          }
58:      }
59:      }
60:  }

Upvotes: 1

Views: 417

Answers (1)

atk
atk

Reputation: 9314

Code review comments....

  • Your indentation is inconsistent. Making it consistent will make it far easier to read your code. It doesn't particularly matter what the standard is, but you should have a standard, though I recommend following the Java standard when writing Java code.
  • Just like writing prose, whitespace matters. Group code that does similar stuff together, and use newlines to separate them, just like you would with paragraphs. This will make your code much easier to read - both for you and for others. There generally should not be empty lines between comments and the code they comment.
  • You have a loop at line 25 to pre-process the file and figure out how many lines you have. I suspect you did this because you are using arrays, and you have not yet learned about classes like Vector. For a homework assignment, this doesn't matter, but if this were production code called thousands of times, it could turn into a bottleneck. You'll eventually be taught about how to write efficient code, so consider this comment a very brief introduction to the topic.
  • Name your variables to represent what they really contain. For example, teamsArray seems to contain a line from PermierShipTeamsOrPlayers.txt, which seems to represent the results of a single game. An Naming it "teamsArray" implies that it contains an array - a list - of a bunch of different teams. A better name would be gameResult. Also, you don't need to specify the type of the variable in the variable name. Look up "reverse polish notation" and you'll see how placing the type in the name is generally a code maintenance problem.

Upvotes: 1

Related Questions