【问题标题】:Displaying Stats with arrays in Java在 Java 中使用数组显示统计信息
【发布时间】:2012-04-25 01:41:51
【问题描述】:

我正在尝试使用 Java 中的数组显示来自简单文本文件的统​​计信息。我知道我应该做什么,但我真的不知道如何编码。那么任何人都可以向我展示如何执行此操作的示例代码。

假设文本文件名为 gameranking.txt,其中包含以下信息(这是一个简单的 txt 文件,用作示例):

Game Event, 1st place, second place, third place, fourth place
World of Warcraft, John, Michael, Bill, Chris
Call of Duty, Michael, Chris, John, Bill
League of Legends, John, Chris, Bill, Michael. 

我的目标是显示统计数据,例如有多少第一名、第二名……每个人在如下表中获胜

Placement     First place, second, third, fourth
John            2            0       1      0
Chris           0            2       0      1
etc...       

我的想法:
首先,我会阅读 gameranking.txt 并将其存储到“输入”。然后我可以使用while循环读取每一行并将每一行存储到一个名为“line”的字符串中,然后使用数组方法“split”提取每个字符串并将它们存储到单独的数组中。之后,我会计算每个人赢得的位置,并使用 printf 将它们显示在一个整洁的表格中。

我的第一个问题是我不知道如何为这些数据创建数组。我是否首先需要通读文件并查看每行和每列中有多少个字符串,然后相应地创建数组表?或者我可以在读取每个字符串时将它们存储在一个数组中吗?

我现在的伪代码如下。

  • 计算有多少行并将其存储在行中
  • 计算有多少列并将其存储在列中
  • 创建数组
  • String [] [] gameranking = new String [row] [column]
  • 接下来读取文本文件并将信息存储到数组中

使用:

while (input.hasNextLine) {
    String line = input.nextLine();
    while (line.hasNext()) {
        Use line.split to pull out each string
        first string = event and store it into the array
        second string = first place
        third string =......
  • 在代码中的某处,我需要计算展示位置......

有人可以告诉我我应该怎么做吗?

【问题讨论】:

  • 是否需要对输入文件的格式进行错误检查?例如,可能会列出另一个游戏,但后面只有 3 个名称(而不是 4 个)。
  • 我暂时不需要做错误检查。文本文件几乎都是这种格式。

标签: java arrays statistics


【解决方案1】:

因为它是一个文本文件,所以使用 Scanner 类。 它可以自定义,以便您可以逐行,逐字或自定义分隔符阅读内容。

readfromfile 方法一次读取一个纯文本文件。

 public static void readfromfile(String fileName) {
  try {
   Scanner scanner = new Scanner(new File(fileName));
   scanner.useDelimiter(",");
   System.out.println(scanner.next()); //instead of printing, take each word and store them in string array
   scanner.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }

这会让你开始。

【讨论】:

  • 这没有回答问题。他正在询问如何创建包含从输入文件生成的统计信息的数组。解析输入只是第一步。
  • @LMV,感谢您的意见。我现在遇到的问题是如何创建数组表并将每个字符串存储到数组表中。我是不是先确定数组的维度,比如5 x 4?然后,我创建数组表。但是如何将每个字符串存储到数组中?我是使用 for 循环还是其他方法?谢谢。
【解决方案2】:

我不会写完整的程序,但我会尝试解决每个问题并给你一个简单的建议:

读取初始文件,您可以使用 BufferedReader 获取每一行并将其存储在字符串中(或者如果您愿意,可以使用 LineNumberReader)

BufferedReader br = new BufferedReader(new FileReader(file));
String strLine;
while ((strLine = br.readLine()) != null)   {
......Do stuff....
}

此时,在 while 循环中,您将遍历字符串(因为它是逗号分隔的,您可以使用它来分隔每个部分)。对于每个子字符串,您可以 a) 将其与第一、第二、第三、第四进行比较以获得位置。 b) 如果不是其中任何一个,那么它可能是游戏名或用户名

您可以通过位置或第 n 个子字符串来计算(即,如果这是第 5 个子字符串,它可能是第一个游戏名称。由于您有 4 个玩家,下一个游戏名称将是第 10 个子字符串,等等) .请注意,我忽略了“游戏事件”,因为这不是模式的一部分。您可以使用 split 来执行此操作或许多其他选项,而不是尝试解释我会给您一个指向我找到的教程的链接: http://pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString.html

至于制表结果,基本上你可以为每个玩家获取一个 int 数组,用于跟踪他们的第 1、2、3、奖励等。

int[] Bob = new int[4]; //where 0 denotes # of 1st awards, etc.
int[] Jane = new int[4]; //where 0 denotes # of 1st awards, etc.

显示表格是组织数据和在 GUI 中使用 JTable 的问题: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

好吧...这是我写的,我相信有一个更清洁和更快的方法,但这应该给你一个想法:

String[] Contestants = {"Bob","Bill","Chris","John","Michael"};

int[][] contPlace=new int[Contestants.length][4];
String file = "test.txt";

public FileParsing() throws Exception {
    Arrays.fill(contPlace[0], 0);
    Arrays.fill(contPlace[1], 0);
    Arrays.fill(contPlace[2], 0);
    Arrays.fill(contPlace[3], 0);
    BufferedReader br = new BufferedReader(new FileReader(file));
    String strLine;
    while((strLine=br.readLine())!=null){
        String[] line = strLine.split(",");
        System.out.println(line[0]+"/"+line[1]+"/"+line[2]+"/"+line[3]+"/"+line[4]);
        if(line[0].equals("Game Event")){
            //line[1]==1st place;
            //line[2]==2nd place;
            //line[3]==3rd place;
        }else{//we know we are on a game line, so we can just pick the names
            for(int i=0;i<line.length;i++){
                for(int j=0;j<Contestants.length;j++){
                    if(line[i].trim().equals(Contestants[j])){
                        System.out.println("j="+j+"i="+i+Contestants[j]);
                        contPlace[j][i-1]++; //i-1 because 1st substring is the game name
                    }

                }
            }
        }
    }
    //Now how to get contestants out of the 2d array
    System.out.println("Placement  First Second Third Fourth");
    System.out.println(Contestants[0]+" "+contPlace[0][0]+" "+contPlace[0][1]+" "+contPlace[0][2]+" "+contPlace[0][3]);
    System.out.println(Contestants[1]+" "+contPlace[1][0]+" "+contPlace[1][1]+" "+contPlace[1][2]+" "+contPlace[1][3]);
    System.out.println(Contestants[2]+" "+contPlace[2][0]+" "+contPlace[2][1]+" "+contPlace[2][2]+" "+contPlace[2][3]);
    System.out.println(Contestants[3]+" "+contPlace[3][0]+" "+contPlace[3][1]+" "+contPlace[3][2]+" "+contPlace[3][3]);
    System.out.println(Contestants[4]+" "+contPlace[4][0]+" "+contPlace[4][1]+" "+contPlace[4][2]+" "+contPlace[4][3]);

}

如果您需要填充参赛者数组或跟踪比赛,则必须插入适当的代码。另请注意,如果您想做除了显示它们之外的任何事情,使用这种二维数组方法可能不是最好的。你应该可以获取我的代码,添加一个 main,然后查看它的运行情况。

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多