0

I'm having trouble adding both string and int values to an arraylist. I'm reading in data from a tsv file, [String String String int] format. It works fine when all tokens are Strings, but I need to have the int to do some calculations later on.

When I try to use an int value it doesn't work. Should I try to add the int to the arraylist or parse afterwards?

Thanks for any help!

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());
}
2
  • You should also post the error you get to it easier to answer your question. Commented Oct 16, 2013 at 11:43
  • Thanks, will do in future Commented Oct 16, 2013 at 12:47

5 Answers 5

3

You must know, that:

  1. You cannot add primitives to a List.
  2. The objects within a List should be of one type.

As a workaround, you can do:

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

This will transform the String to int and you will be able to assign it to your PlaycountData object.

Sign up to request clarification or add additional context in comments.

3 Comments

Ideally, what should I do? The workaround works, but obviously I shouldn't use a workaround if I can help it. Thanks for your help, much appreciated
Well....this is solution is fine. You can use it everytime you need to convert a String to an int. It's better that other possible approaches. But be carefull with the content of the String. If an int cannot be created, a NumberFormatException will be thrown. Also, pay a special attention on the first two notes.
In this case, you can use Integer for playCount field.
1

Try converting the String to an int.

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

Comments

0

You already add to your List the PlaycountData object, which looks fine. What you need is to construct the object in the right way.

This line

//this.playcount = rep[3];

should probably then read

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

Comments

0

You need to parse the String to an int to assign the value to your playcount in your PlaycountData you're creating.

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
}

Comments

0

Other way to achieve this to convert String token to integer as

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.