Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 - Fatal编程技术网

Java 从具有多个数据类型的文本文件创建数组

Java 从具有多个数据类型的文本文件创建数组,java,arrays,Java,Arrays,首先是一些背景: 主要方法将通过执行以下操作来驱动程序: 创建一个数组来保存所有的高尔夫球员和阿纳尔分数(类型是高尔夫球员)。 提示用户输入包含PAR得分和球员姓名和分数的数据文件。输入文件的格式应如下所示: 第3、4、4、3、4、3、5、3、4杆 乔治,3,3,4,3,4,2,3,3,4 保罗,4,5,4,3,6,3,4,3,5 林戈,3,3,4,3,4,2,4,3,4 约翰,4,4,4,3,4,2,5,3,4 这就是我到目前为止所做的: import java.io.File; import

首先是一些背景:

主要方法将通过执行以下操作来驱动程序: 创建一个数组来保存所有的高尔夫球员和阿纳尔分数(类型是高尔夫球员)。 提示用户输入包含PAR得分和球员姓名和分数的数据文件。输入文件的格式应如下所示:

第3、4、4、3、4、3、5、3、4杆

乔治,3,3,4,3,4,2,3,3,4

保罗,4,5,4,3,6,3,4,3,5

林戈,3,3,4,3,4,2,4,3,4

约翰,4,4,4,3,4,2,5,3,4

这就是我到目前为止所做的:

import java.io.File;
import java.io.IOException;
import java.util.*;

public class Main 

{

public static void main(String[] args)
{
    boolean tryAgain;

       do
       {
           Scanner console = new Scanner( System.in );
           System.out.print( "Please enter a file name to read: " );
           String inFile = console.next();
           tryAgain = false;

           try
           {
               File file = new File( inFile );     
               Scanner tokens = new Scanner( file ); 

                int data[][];
                String str = "";

               for (int i = 0; i < 5; i++)
               {
                   for (int j = 0; j < 10; j++)
                   {

                       int value = tokens.nextInt();


                   }
                   System.out.printf("\n");
               }

               tokens.close();

               String people [] = str.split(",");
               Golfer golfers = new Golfer(null, null);
           }


           catch (IOException e)
           {
               System.out.printf("Error opening file %s, %s\n", inFile, e);
               tryAgain = true;
           }
       }
       while (tryAgain);


}
导入java.io.File;
导入java.io.IOException;
导入java.util.*;
公共班机
{
公共静态void main(字符串[]args)
{
布尔图里加因;
做
{
扫描仪控制台=新扫描仪(System.in);
System.out.print(“请输入要读取的文件名:”);
字符串infle=console.next();
tryAgain=false;
尝试
{
文件=新文件(填充);
扫描器令牌=新扫描器(文件);
int数据[];
字符串str=“”;
对于(int i=0;i<5;i++)
{
对于(int j=0;j<10;j++)
{
int value=tokens.nextInt();
}
System.out.printf(“\n”);
}
tokens.close();
字符串people[]=str.split(“,”);
高尔夫球手=新高尔夫球手(空,空);
}
捕获(IOE异常)
{
System.out.printf(“打开文件%s,%s\n”,infle,e时出错);
tryAgain=true;
}
}
while(tryAgain);
}
我不确定如何正确使用嵌套for循环来提取数据并将其存储在数组中,因为有两种数据类型。而且……我想任何关于如何在我的高尔夫课程中存储信息的建议都不会有什么坏处。我是一个初学者,所以应该避免使用任何复杂的技术

如有任何建议,将不胜感激。
谢谢!

在这种情况下,需要创建一个时间来保存单行中的所有数据,例如名称,并创建一个数组来保存该特定高尔夫球手的所有得分

while(tokens.hasNext()) {
    String input = token.nextLine();
    String [] splitGolfer = input.split(",");
    Golfer newGolfer = new Golfer();
    newGolfer.name = splitGolfer[0];
    ArrayList<Integer> scores = new ArrayList<Integer>();
    for(int i= 1; i < splitgolfer.length; i++) {
        scores.add(Integer.valueOf(splitGolfer[i]);
    }
    newGolfer.scores = scores;
while(tokens.hasNext()){
字符串输入=token.nextLine();
String[]splitGolfer=input.split(“,”);
高尔夫球手newGolfer=新高尔夫球手();
newGolfer.name=splitGolfer[0];
ArrayList分数=新建ArrayList();
对于(int i=1;i
高尔夫球手等级:

String name;
ArrayList<Integer> scores;
字符串名;
ArrayList分数;

这是适合您的解决方案。根据您的要求更改变量、文件名和目录。 如果文件中的任何一行的格式与上面提到的不同(玩家的名字后面跟着分数),它将抛出一个异常

import java.util.*;
导入java.io.*;
公营高尔夫球手
{
//给定文件(玩家数据)中所有行的arraylist。
ArrayList theData=新的ArrayList();
私人弦乐演奏者姓名;
私人ArrayList parScores;
//您必须读取golder文件中的每一行,并将其单独发送到此方法。
公共高尔夫球手()抛出异常
{
字符串fileName=“players.txt”;
数据=读取文件(文件名);
用于(字符串s:数据)
{
getPlayerData(s);
}
}
公共无效getPlayerData(字符串数据)
{
String[]list=data.split(“,”);
playerName=list[0];
parScores=newarraylist();
for(int i=1;i
您需要一个类型;
高尔夫球手
。也许可以编写一个方法将一行解析为
高尔夫球手
。将其放在一个对象上,然后在一个条件下使用
instanceof
很抱歉,但我不太确定如何正确执行这两个操作。您可能希望按“,”进行拆分有没有不使用ArrayList的方法可以做到这一点?
import java.util.*;
import java.io.*;

public class Golfer
{
    //arraylist of all lines from given files (players data).
    ArrayList<String> theData = new ArrayList<String>();
    private String playerName;
    private ArrayList<Integer> parScores;

    //you must read each line in the golder file and send it individually to this method.
    public Golfer() throws IOException
   {
        String fileName = "players.txt";
        theData = readFile(fileName);
        for (String s : theData)
        {
            getPlayerData(s);
        }
    }

    public void getPlayerData(String data)
    {
        String[] list = data.split(",");
        playerName = list[0];
        parScores = new ArrayList<Integer>();
        for( int i = 1; i < list.length; i++)
        {
            parScores.add(Integer.parseInt(list[i]));
        }
    }

    //This method will read your data file and will return arraylist (one array for each player).
    public ArrayList<String> readFile(String fileName) throws IOException
    {
        ArrayList<String> data = new ArrayList<String>();
        //don't forget to add directory name here if you have, because we are only passing filename, not directory.
        BufferedReader in = new BufferedReader(new FileReader(fileName));

        String temp = in.readLine(); 
        while (temp != null)
        {
            data.add(temp);
            temp = in.readLine();
        }
        in.close();
        return data;
    }
}