2017-04-09 52 views
0
內一個新的對象

有人能幫助我輸入該數據爲3個對象在一個ArrayList(每個人)?讀取文本文件(文件選擇器),並存儲到一個ArrayList(每行的ArrayList

文本文件的例子:

Steve| Barkley| 258| 300 
Carl |Johnson |142 
Frank|Davidson 

Java代碼:

//couldn't write the normal jfilechoose code above due to space 
    File playerFile = new File(selectedFile.getAbsolutePath()); 

    Scanner in = new Scanner(playerFile); 

    String[] playerData; //array to hold data 

    while (in.hasNext()) { 
     String data = in.nextLine(); 

     playertData = data.split("\\|"); 
     playerData = Arrays.copyOf(playerData,playerData.length+1); 

     String firstName = playerData[0]; 
     String lastName = playerData[1]; 

     double playererayear1 = Double.parseDouble(playerData[2]==null?"0":playerData[2]); 
     double playererayear2 = Double.parseDouble(playerData[3]==null?"0":playerData[3]); 

     double playererayear3 = Double.parseDouble(playerData[4] == null?"0":playerData[4]); 
+0

是什麼studentdata這裏? – prasanth

+0

啊我無意中把名字改成了學生,而我正在做testruns,但它仍然是playerData虐待試圖看看我是否可以編輯問題 – Alexa431

+0

你想要一個'ArrayList '或'ArrayList '嗎? – prasanth

回答

0

您可以持有關球員信息創建一個單獨的類

public class Player { 
    private String firstName; 
    private String lastName; 
    private double playerEraYear1; 
    private double playerEraYear2; 
    private double playerEraYear3; 

    public String getFirstName() { 
    return firstName; 
    } 

    public void setFirstName(String firstName) { 
    this.firstName = firstName; 
    } 

    public String getLastName() { 
    return lastName; 
    } 

    public void setLastName(String lastName) { 
    this.lastName = lastName; 
    } 

    public double getPlayerEraYear1() { 
    return playerEraYear1; 
    } 

    public void setPlayerEraYear1(double playerEraYear1) { 
    this.playerEraYear1 = playerEraYear1; 
    } 

    public double getPlayerEraYear2() { 
    return playerEraYear2; 
    } 

    public void setPlayerEraYear2(double playerEraYear2) { 
    this.playerEraYear2 = playerEraYear2; 
    } 

    public double getPlayerEraYear3() { 
    return playerEraYear3; 
    } 

    public void setPlayerEraYear3(double playerEraYear3) { 
    this.playerEraYear3 = playerEraYear3; 
    } 
} 

現在你可以分析該文件,創建一個Player對象爲每個球員和他們添加到列表

public void parseFile() { 
    File playerFile = new File(selectedFile.getAbsolutePath()); 
    Scanner in = new Scanner(playerFile); 
    List<Player> players = new ArrayList<>(); 
    while (in.hasNext()) { 
     String data = in.nextLine(); 
     String[] playerData = data.split("\\|"); 

     Player p = new Player(); 
     p.setFirstName(playerData[0]); 
     p.setLastName(playerData[1]); 

     if (playerData.size >= 3) { 
     double playererayear1 = Double.parseDouble(playerData[2] == null ? "0" : playerData[2]); 
     p.setPlayerEraYear1(playererayear1); 
     } 

     if (playerData.size >= 4) { 
     double playererayear2 = Double.parseDouble(playerData[3] == null ? "0" : playerData[3]); 
     p.setPlayerEraYear2(playererayear2); 
     } 

     if (playerData.size >= 5) { 
     double playererayear3 = Double.parseDouble(playerData[4] == null ? "0" : playerData[4]); 
     p.setPlayerEraYear3(playererayear3); 
     } 

     players.add(p); 
    } 
} 
+0

不幸的是我得到了:選中文件:C:\用戶\約克\文檔\ PlayerData.txt 異常線程 「main」 java.lang.ArrayIndexOutOfBoundsException:4 \t在PlayerList.parseFile(PlayerList.java:44) \t在Driver.main(Driver.java:6) – Alexa431

+0

我意味着你沒有你的文本文件中的最後一列。你在每個球員的文本文件中有多少列?它會是5嗎? – prasanth

+0

如果它不會是每行的相同列數,您可以在設置playerEraYear的第1,2和3年之前添加支票。 – prasanth

0
package com.aegle.validator; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import java.util.Scanner; 

public class Test { 
    public static void main(String[] args) throws FileNotFoundException { 
     List<Player> playerList = new ArrayList<Player>(); 
     File playerFile = new File("");//Set your file path 
     Scanner in = new Scanner(playerFile); 

     while (in.hasNext()) { 
      String[] data = in.nextLine().split("\\|"); 
      Player player = new Player(data[0], data[1]); 
      player.setYears(Arrays.copyOfRange(data, 2, data.length)); 
      playerList.add(player); 
     } 

     System.out.println(playerList);//Just to test 
    } 
} 

class Player { 
    String firstName; 
    String lastName; 
    String[] years; 

    public Player(String firstName, String lastName) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
    } 

    public void setYears(String[] years) { 
     this.years = years; 
    } 

    //Introduce getters as you need 

    @Override 
    public String toString() { 
     return "Player{" + 
       "firstName='" + firstName + '\'' + 
       ", lastName='" + lastName + '\'' + 
       ", years=" + Arrays.toString(years) + 
       '}'; 
    } 
} 
相關問題