将String和int值添加到Arraylist - Java

时间:2013-10-16 11:38:33

标签: java arraylist

我在将字符串和int值添加到arraylist时遇到了问题。我正在读取tsv文件中的数据,[String String String int]格式。当所有令牌都是字符串时,它工作正常,但我需要让int稍后进行一些计算。

当我尝试使用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());
}

5 个答案:

答案 0 :(得分:3)

必须知道:

  1. 无法将基元添加到List
  2. List中的对象应为一种类型。
  3. 作为解决方法,您可以执行以下操作:

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

    这将转换 Stringint,您就可以将其分配给PlaycountData对象了。

答案 1 :(得分:1)

尝试将String转换为int。

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

答案 2 :(得分:0)

您已经在List中添加了PlaycountData对象,看起来很好。你需要的是以正确的方式构建对象。

这一行

//this.playcount = rep[3];

应该阅读

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

答案 3 :(得分:0)

您需要将String解析为int,以便为您正在创建的playcount中的PlaycountData分配值。

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
}

答案 4 :(得分:0)

实现此目的的其他方法将String标记转换为整数

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