2013-10-16 78 views
0

我無法將字符串和整型值添加到數組列表中。我正在從tsv文件[String String String int]格式讀取數據。當所有的令牌都是字符串時它工作正常,但我需要讓int在稍後進行一些計算。將字符串和整型值添加到Arraylist - Java

當我嘗試使用一個int值時,它不起作用。我應該嘗試將int添加到arraylist還是事後解析?

感謝您的幫助!

public class PlaycountData { 

//declare variables 
    public String userID; 
    public String artistID; 
    public String artistName; 
    public int playcount; 

    public PlaycountData(String userID, String artistID, String artistName, int playcount){ 
     this.userID = userID; 
     this.artistID= artistID; 
     this.artistName= artistName; 
     this.playcount= playcount; 
    } // end constructor 


    public PlaycountData(String[] rep) { 
     this.userID = rep[0]; 
     this.artistID = rep[1]; 
     this.artistName = rep[2]; 
     //this.playcount = rep[3]; 
    } 




    // getter methods 
    public String getUserID(){ 
     return userID; 
    } 

    public String getArtistID(){ 
     return artistID; 
    } 

    public String getArtistName(){ 
     return artistName; 
    } 

    public int getPlaycount(){ 
     return playcount; 
    } 

    // setter methods 
    public void setUserID(String userID){ 
     this.userID = userID; 
    } 

    public void setArtistID(String artistID){ 
     this.artistID = artistID; 
    } 

    public void setArtistName(String artistName){ 
     this.artistName = artistName; 
    } 

    public void setPlaycount(int playcount){ 
     this.playcount = playcount; 
    } 

    @Override 
    public String toString(){ 
     return userID + artistID + artistName + playcount; 
    } 
} 



public class LastFm { 

static ArrayList<PlaycountData> playcountRecords; 

public static void main(String [ ] args) throws IOException{ 

playcountRecords = new ArrayList<PlaycountData>(); // create a new arraylist of type PlaycountData 

String fileName = "C:\\Users\\Siân\\Desktop\\sample2.tsv"; 

BufferedReader br = new BufferedReader(new FileReader(fileName)); // allows us to read contents of file 

String line; 

try{ 
    while((line = br.readLine()) != null){ // keep reading if there are more lines in the file 
     String[] rep = line.split("\t"); // enter the split line into an array 
     PlaycountData playrep = new PlaycountData(rep); // create new PlaycountData object 
     playcountRecords.add(playrep); // add array to arraylist 
    } }catch(IOException e){ 
    System.err.println("An error has occurred" + e); 
    System.exit(-1); 
} finally{ 
    if(br != null) 
     br.close(); // close the stream 
} 

for(PlaycountData pr: playcountRecords){ 
    System.out.println(pr.getUserID() + " " + pr.getArtistID() + " " + pr.getArtistName() 
      + " " + pr.getPlaycount()); 
} // outputs all the data 

int[] plays = new int[playcountRecords.size()]; // create an int array to hold the playcounts 

for(PlaycountData pr2: playcountRecords){ 
    System.out.println(pr2.getPlaycount()); 
} 
+0

你也應該張貼你能更容易地回答你的問題的錯誤。 – Axel

+0

什麼是錯誤? – Cold

+0

謝謝,未來會做 – user2886192

回答

1

嘗試將字符串轉換爲int。

this.playcount = Integer.parseInt(rep[3]); 
3

必須知道,那:

  1. 不能加元到List
  2. List中的對象應該是一種類型。

作爲一種變通方法,你可以這樣做:

this.playcount = Integer.parseInt(rep[3]); 

這將變換Stringint,你就可以將其分配給您的PlaycountData對象。

+0

理想情況下,我該怎麼辦?解決方法有效,但顯然我不應該使用解決方法,如果我可以幫助它。感謝您的幫助,非常感謝 – user2886192

+0

那麼....這是解決方案很好。您可以在每次需要將「String」轉換爲「int」時使用它。其他可能的方法更好。但要小心字符串的內容。如果不能創建「int」,則會拋出NumberFormatException異常。另外,請特別注意前兩個音符。 –

+0

在這種情況下,您可以使用'Integer'作爲'playCount'字段。 – Cold

0

您已將PlaycountData對象添加到您的列表中,看起來不錯。你需要的是以正確的方式構建對象。

此行

//this.playcount = rep[3]; 

也許應該再讀取

this.playcount = Integer.parseInt(rep[3]); 
0

您需要的String解析到int的值賦給你playcountPlaycountData你創建。

public PlaycountData(String[] rep) { 
     this.userID = rep[0]; 
     this.artistID = rep[1]; 
     this.artistName = rep[2]; 
     this.playcount = Integer.parseInt(rep[3]); // Uncomment this and parse the string to int 
} 
0

其他方式實現這一目標,以字符串令牌轉換爲整數

this.playcount = Integer.valueOf(rep[3]);