Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java中的数组显示统计信息_Java_Arrays_Statistics - Fatal编程技术网

用Java中的数组显示统计信息

用Java中的数组显示统计信息,java,arrays,statistics,Java,Arrays,Statistics,我试图用Java中的数组显示一个简单文本文件中的统计信息。我知道我应该做什么,但我真的不知道如何编码。所以,谁能给我看一个如何做的示例代码 假设文本文件名为gameranking.txt,包含以下信息(这是一个简单的txt文件,用作示例): 我的目标是显示统计数据,例如排名第一、第二的人数。。每个人在如下表格中获胜 Placement First place, second, third, fourth John 2 0 1

我试图用Java中的数组显示一个简单文本文件中的统计信息。我知道我应该做什么,但我真的不知道如何编码。所以,谁能给我看一个如何做的示例代码

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

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

Placement     First place, second, third, fourth
John            2            0       1      0
Chris           0            2       0      1
etc...       
我的想法:
首先,我会阅读gameranking.txt并将其存储到“input”中。然后,我可以使用while循环读取每一行并将每一行存储到一个名为“line”的字符串中,然后,我将使用数组方法“split”提取每一行并将它们存储到单个数组中。之后,我会计算每个人赢得的排名,并使用printf将其显示在一个整洁的表格中

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

我现在拥有的伪代码如下

  • 计算有多少行并将其存储在第行中
  • 计算有多少列并将其存储在列中
  • 创建一个数组
  • String[][]游戏排名=新字符串[行][列]
  • 接下来读取文本文件并将信息存储到数组中
使用:

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 =......
  • 在代码的某个地方,我需要计算位置

谁能告诉我该怎么做

由于它是一个文本文件,请使用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();
  }
 }

这将让您开始。

由于它是一个文本文件,请使用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();
  }
 }

这将让您开始。

我不打算编写完整的程序,但我将尝试解决每个问题,并给您一个简单的建议:

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

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

您可以通过位置或第n个子串(即,如果这是第5个子串,它很可能是第一个游戏名称。因为您有4名玩家,下一个游戏名称将是第10个子串,以此类推)。请注意,我忽略了“游戏事件”,因为这不是模式的一部分。您可以使用“拆分”来执行此操作或使用许多其他选项,而不是试图解释我将为您提供指向我找到的教程的链接:

至于制表结果,基本上你可以为每个玩家得到一个整数数组来记录他们的第一、第二、第三等奖项

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的问题:

好的……这是我写的,我相信有一个更干净、更快的方法,但这应该给你一个想法:

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

}
String[]参赛者={“鲍勃”、“比尔”、“克里斯”、“约翰”、“迈克尔”};
int[][]contPlace=新int[参赛者.长度][4];
String file=“test.txt”;
public FileParsing()引发异常{
数组.fill(contPlace[0],0);
数组.fill(contPlace[1],0);
数组.fill(contPlace[2],0);
数组.fill(contPlace[3],0);
BufferedReader br=新的BufferedReader(新文件读取器(文件));
弦斯特林;
而((strLine=br.readLine())!=null){
String[]line=strLine.split(“,”);
System.out.println(第[0]+“/”+第[1]+“/”+第[2]+“/”+第[3]+“/”+第[4]行);
如果(行[0]。等于(“游戏事件”)){
//第[1]行==第1位;
//第[2]行==第2位;
//第[3]行==第3位;
}否则{//我们知道我们在一条战线上,所以我们可以选择名字

对于(int i=0;i我不打算编写完整的程序,但我将尝试解决每个问题,并给您一个简单的建议:

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

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

你可以通过位置或第n个子串(即,如果这是第5个子串,它可能是第一个游戏名称。因为你有4名玩家,下一个游戏名称将是第10个子串,等等)。注意,我忽略了“游戏事件”因为这不是模式的一部分。您可以使用“拆分”来执行此操作或其他许多选项,而不是试图解释我将为您提供指向我找到的教程的链接:

至于制表结果,基本上你可以为每个玩家得到一个整数数组来记录他们的第一、第二、第三等奖项

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的问题:

好的……这是我写的,我相信有一个更干净、更快的方法,但这应该给你一个想法:

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

}
String[]参赛者={“鲍勃”、“比尔”、“克里斯”、“约翰”、“迈克尔”};
int[][]contPlace=新int[参赛者.长度][4];
String file=“test.txt”;
public FileParsing()引发异常{
数组.fil