2014-12-04 121 views
0

首先是一些背景阵列:创建从一个文本文件与多个数据类型

主要方法做驱动程序如下: •创建一个数组来保存所有的个体高尔夫球手和参数分数(类型是Golfer [])。 •提示用户输入包含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); 


} 

我不确定如何正确使用嵌套的for循环来提取数据,并将其存储在一个数组,因为有两个数据类型。而且......我想如何将信息存储在我的高尔夫球员课程中的任何建议都不会受到伤害。我是初学者,所以任何复杂的技术都应该避免。

任何意见将不胜感激。 谢谢!

+2

您需要一种类型; 'Golfer'。也许写一个方法来解析一个单行到一个“高尔夫球员”。 – 2014-12-04 02:35:18

+0

把它放在一个对象,然后在条件:)使用'instanceof' – Secondo 2014-12-04 02:43:41

+0

我很抱歉,但我不太清楚如何正确执行这些事情之一。 – 2014-12-04 02:43:42

回答

2

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

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; 

高尔夫球类:

String name; 
ArrayList<Integer> scores; 
+0

你可能想要拆分“,” – 2014-12-04 02:55:19

+0

有没有办法做到这一点,而不使用ArrayList? – 2014-12-04 02:59:11

0

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

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; 
    } 
} 
相关问题