【问题标题】:Store text file info to an array将文本文件信息存储到数组
【发布时间】:2013-03-20 00:18:38
【问题描述】:

我正在尝试显示到目前为止所玩游戏的结果,我有 3 个文本文件(resultsfixturesteamsOrPlayers)。我希望能够将teamNames 读入一个数组,然后能够描绘出像(阿森纳 2:1 曼城)这样的结果

 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:  }

【问题讨论】:

  • 你遇到了什么问题?
  • 我不确定如何将项目存储到数组中......可以将数据存储在数组中
  • 这是整个程序还是其中的一部分?
  • 是的,这是整个程序
  • 好的...您可以评论您的代码,以便我们知道您要在代码中做什么。有很多错误,但如果你甚至不确定每一位“应该”做什么,那么经历它们将会很痛苦。例如,第一个 while 循环应该做什么?你在哪里声明 lineFromFile 和 TeamsArray?到目前为止,您查阅了哪些文件?等等 另外,为什么有两个同名的 Scanner 对象?

标签: java arrays text


【解决方案1】:

代码审查 cmets....

  • 您的缩进不一致。使其保持一致将使您的代码更容易阅读。标准是什么并不重要,但你应该有一个标准,尽管我建议在编写 Java 代码时遵循 Java 标准。
  • 就像写散文一样,空格很重要。将执行类似操作的代码组合在一起,并使用换行符分隔它们,就像使用段落一样。这将使您的代码更易于阅读 - 对您和其他人都是如此。 cmets 和它们注释的代码之间通常不应有空行。
  • 您在第 25 行有一个循环来预处理文件并计算出您有多少行。我怀疑您这样做是因为您使用的是数组,并且您还没有了解 Vector 之类的类。对于家庭作业,这无关紧要,但如果这是调用数千次的生产代码,它可能会变成瓶颈。您最终将学会如何编写高效的代码,因此请将此评论视为对该主题的非常简短的介绍。
  • 为变量命名以表示它们真正包含的内容。例如,teamArray 似乎包含来自 PermierShipTeamsOrPlayers.txt 的一行,它似乎代表了单场比赛的结果。将其命名为“teamsArray”意味着它包含一组不同团队的数组 - 一个列表。一个更好的名字是gameResult。此外,您不需要在变量名中指定变量的类型。查找“反向波兰表示法”,您会发现在名称中放置类型通常是一个代码维护问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多