2014-12-04 70 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; 
    } 
}