I have a file that looks like this:
1st 2nd nth
e1 v1 1
e1 v3 2
e1 v4 4
e1 v5 7
e2 v1 1
. . .
. . .
. . .
I want the first column to be the name of the arraylist (e1, or e2, or e3), I want the second column to have it’s value (an int), inside the nth index.
How would I go about doing that?
Id you’d like further clarification on what I mean, please don’t be afraid to leave a pm.
p.s. This is part of a bigger project, but I’m kind of stuck at the format of the data.
I’m assuming i’ll have to look for an arraylist with the the name (first column) and if i find it, then add the value (2nd column) at the nth value (3rd column), and if I don’t find it, I make an arrayList, and THEN do the aforementioned.
Update
So I managed to get the program almost working — I couldn’t use the original structure of ArrayLists because all variables have to be declared during compilation — can’t be declared after.
I decided to use a hashmap, with the key has the 1st value, and an arraylist of the 2nd values in the nth position:
`HashMap<String, ArrayList<Integer>> userRatings = new HashMap<String, ArrayList<Integer>>();
if(!userRatings.containsKey(user)) //The user does not have ratings.
{
ArrayList<Integer> ratings = new ArrayList<Integer>();
// ratings = userRatings.get(user);
for (int j = 0; j < 50; j++)
{
ratings.add(j);
}
userRatings.get(user).add(location,value);
} }
else //The user has ratings
{
userRatings.get(user).add(location,value);**
}
System.out.println(user + " " + userRatings.get(user));
}
bufferReader.close();
} catch (FileNotFoundException e)
{
System.out.println("File does not exist or could not be found.");
}
catch (IOException e)
{
System.out.println("Can't read from file");
}
catch (NullPointerException e)
{
}`
The problem I now have is in adding to the arraylist. How would I do that in the bolded areas? — How would I populate and manipulate the arraylist?
This is easy if you use the right library. Take a look at this one: http://opencsv.sourceforge.net/, which is meant to parse csv files. From that site:
I think this is exactly what you’re trying to do. You’re asking this library to parse the input file with tab-delimited values instead of comma-delimited. From your example above, that seems to be what your input format is.