2015-02-11 68 views
2

我現在有一個數據庫訪問類拉從我的名單像這樣列出的數據:獲取列表從一個文本文件中的java

Pique:CBPique.png:41:22:55:91 
Ronaldo:STRonaldo.png:89:85:92:91 
... 

類如下:

class DatabaseAccess { 




static DatabaseAccess dataAccessor; 


static DatabaseAccess getInstance(String dbPath) { 

    if (dataAccessor == null) { 
     dataAccessor = new DatabaseAccess(dbPath); 
    } 

    return dataAccessor; 
} 


private DatabaseAccess(String dbPath) { 
    dbLocation = dbPath; 
} 


List<FootballPlayer> getCards() { 
    return this.getData(); 
} 


private List<FootballPlayer> getData() { 

    List<FootballPlayer> theData = new ArrayList<FootballPlayer>(); 

    // create a Scanner and grab the data . . . 

    Scanner scanner = null; 

    String dataPath = dbLocation + File.separator + "text" + File.separator + "players.db"; 

    String imagePath = dbLocation + File.separator + "images"; 

    try { 

     scanner = new Scanner(new File(dataPath)); 

    } catch (FileNotFoundException fnf) { 

     System.out.println(fnf.getMessage()); 


     System.exit(0); 
    } 

    // scan players.db file line-by-line 

    scanner.useDelimiter("\n"); 

    while (scanner.hasNext()) { 

     String line = scanner.next().trim(); 

     // trim used to trim for new line 

     String[] bits = line.split(":"); 

     String t = bits[0];     // title 
     String imgFileName = bits[1];   // image file name 
     int pa = Integer.parseInt(bits[2]);  // pace 
     int sh = Integer.parseInt(bits[3]);  // shooting 
     int dr = Integer.parseInt(bits[4]); // dribbling 
     int ph = Integer.parseInt(bits[5]); // physical 

     // create the image 

     ImageIcon img = new ImageIcon(imagePath + File.separator + imgFileName); 

     // Create the business object 

     FootballPlayer player = new FootballPlayer(t, img, pa, sh, dr, ph); 

     // add it to the list ... simple as ... 

     theData.add(player); 
    } 

    scanner.close(); 

    return theData; 

} 

我想要做的是從這個列表中提取數據並在另一個類中顯示/使用它,例如拉取圖像文件名以供使用,甚至在列表中顯示所有數據。

一直在努力與它和任何幫助將非常感激。

+0

凹凸凹凸任何幫助找回您的播放器? – Nebbyyy 2015-02-11 02:11:10

回答

1

這可能是掃描儀看起來像這樣的一種方式:它讀取文件並將每行行保存到ArrayList中。

ArrayList<String> singleParts = new ArrayList<String>(); 

    try { 
     int index = 0; 
     Scanner scanner = new Scanner(new File("files/"+filename+".txt")); 
     while (scanner.hasNextLine()) { 
      String actualLine = scanner.nextLine(); 
      singleParts.add(actualLine); 
     } 
     scanner.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch(Throwable te) { 
     te.printStackTrace(); 
    } 

在此之後,你可以迭代你的ArrayList行每行char每個字符。爲了使事情更容易,你可以使用':'作爲分隔符。

例第一行,名稱:

String name = ""; 
boolean saved = false; 

for(int line = 0; line < singleParts.size(); line++) { 
    for(int char = 0; char < singleParts.get(line).length(); char++) { 
     if(char<singleParts.get(line).indexOf(':') { 
      name += singleParts.get(line).charAt(char); 
     } 
    } 
} 

爲了得到休息,你有很多的可能性。例如,刪除已經使用的字符並重新使用與此類似的循環。

1

您的Player對象是否具有唯一的屬性/屬性,如名稱或ID?

既然你已經有一個包含所有你需要的屬性Player對象,爲什麼不把它放在一個HashMap而不是Arraylist,考慮到如果你有一個關鍵確定具體Player

FootballPlayer player = new FootballPlayer(t, img, pa, sh, dr, ph); 
yourKey = t // an identifier to you player, in this case i used t for example 
playersMap.put(t,player); 

這樣你就可以很容易地通過使用

Player myPlayer = playersMap.get(yourKey) 

// you can then get the properties of the Player 
myPlayer.getImg(); 
myPlayer.getPa(); 

希望這有助於

相關問題