Problems when parsing file

My parser class should be reading in the data and adding it into 2 arraylist.

If a user is a new user they should be added into the userarraylist.
Each user has a set of pairs associated with them, as each user will have rated several cds.

When a new user is found , then all the cd_ids and the rating for each cd should be added into the users profile.

This process should be repeated for every user in the dataset.

Here is an example taken from the dataset(u is user):

U  Cd_Id  Rating
--------------------
1     17     0.80
1     18     0.20
1     19     0.20
1     21     0.40
1     25     0.80
1     31     0.20
1     32     0.60
1     34     0.80
1     36     0.40
1     39     0.80
1     41     0.60
1     43     0.60
1     44     0.00
1     45     0.80
1     47     0.40
1     50     0.60
1     52     0.40
1     55     0.80
1     57     0.60
1     58     0.40
1     62     0.20
1     66     0.00
1     70     0.60
1     95     0.20
1     96     0.40
1     104     0.00
1     110     0.60
1     111     0.80
1     112     0.20
1     117     0.20
1     141     0.40
1     149     0.00
1     150     0.80
1     151     0.60
1     152     0.20
1     153     0.60
1     154     0.40
1     157     0.00
1     160     0.00
1     161     0.40
1     162     1.00
1     163     0.40
1     165     0.20
1     169     0.00
1     170     0.00
162     1235     0.40
162     1236     0.00
162     1237     0.40
162     1238     0.40
162     1241     0.00
162     1242     0.40
162     1243     1.00
162     1244     0.40
162     1245     0.80
162     1246     0.00
162     1247     0.60
162     1250     0.80
162     1253     0.00
162     1254     0.80
162     1255     0.00
162     1256     0.80
162     1257     0.00
162     1259     0.00
162     1261     0.00
162     1262     0.40
162     1263     0.40
162     1265     0.40
162     1268     0.00
162     1269     0.40
162     1270     0.00
162     1271     0.00
162     1272     0.40
162     1273     0.00
162     1276     0.80
162     1278     0.60
162     1279     0.00
162     1281     0.00
162     1283     0.80
162     1285     0.00
162     1286     0.00
162     1287     0.00
162     1288     0.80
162     1289     0.00
162     1290     0.00
162     1291     0.40
162     1292     0.60
162     1293     0.80
162     1294     0.40
162     1295     0.40
162     1297     0.00
162     1298     0.00
162     1299     0.40
162     1301     0.00
162     1302     0.00
162     1304     0.80
162     1305     0.80
162     1306     0.00
162     1307     0.40
162     1309     0.00
162     1310     0.00
162     1313     0.00
162     1316     0.00
162     1318     0.00
162     1319     0.00
162     1330     0.00
162     1344     0.00
162     1352     0.00
162     1354     1.00
162     1356     0.00
162     1357     0.00
162     1358     0.00
162     1361     0.00
162     1362     0.00
162     1364     0.00
162     1365     0.00
162     1366     0.00
162     1367     0.00
162     1368     0.00
162     1378     0.00
162     1379     0.00
162     1380     0.00
162     1381     0.00
162     1383     0.00
162     1384     0.00
162     1393     0.00
599     1     0.80
599     2     0.00
599     4     0.80
599     5     0.60


Here is my code:

FileParser.java
--------------------

FileReader fr =
      new FileReader("C:/dataset.txt"); // to open the file
      BufferedReader inFile = new BufferedReader(fr);     // to read the file
      String line;     // String variable used to hold each line of the file
      StringTokenizer tokenizer;     // string tokenizer to parse each file line
      ArrayList userArrayList = new ArrayList();
      int id = 0;
      int index = 0;     // Index that we increment each time through the loop
      User user = null;
      int cdId;     // value to hold cd id
      float ratings;     // value to hold cd ratings
      ArrayList pair = null;
      while((line = inFile.readLine()) != null) {
        tokenizer = new StringTokenizer(line);     // tokenize the file line
        // now add the user id to the user id array
        id = Integer.parseInt(tokenizer.nextToken());
        // initialize the cd id variable
        cdId = Integer.parseInt(tokenizer.nextToken());
        // initialize the cd ratings variable
        ratings = Float.parseFloat(tokenizer.nextToken());
        // Check out if the user has already been added to the list
        Iterator iter = userArrayList.iterator();

/*I think this is where the problem is.When i throw in a print statement it only prints out for user1,but i'm unsure as to why it isn't working for the whole dataset.*/



        while (iter.hasNext()) {
          User tmp = (User)iter.next();
          if (tmp.getUserId() == id)
          System.out.println(tmp.getUserId());
            {
              user = tmp;
              break;
            }
        }

        if (user == null) {
          pair = new ArrayList();
          pair.add(new Pair(movieId, ratings));
          user = new User(id,pair);
          userArrayList.add(user);

        } else
          user.getpairArray().add(new Pair(movieId, ratings));

          // incrememnt index
          index++;
        }
LVL 2
override_y2kAsked:
Who is Participating?
 
vk33Commented:
Hi!

The problem is that you've added your debug printout in the if clause:
         User tmp = (User)iter.next();
          if (tmp.getUserId() == id)
          System.out.println(tmp.getUserId());
            {
              user = tmp;
              break;
            }
        }

        if (user == null) {

That's why if the user is found it's only printed out, the tmp variable is not set to reference it. So you'll never get into else statement... :)

Regards!
0
 
override_y2kAuthor Commented:
The problem is that it only adds 1 user and 1 pair.It should add every pair for a user, and do this for every user!
0
 
SunBowCommented:
Sorry, I don't code that way, but you might recheck this sequence:

     User user = null;
...
            user = tmp;
             break;
 ...
       if (user == null) {
 
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
SunBowCommented:
(this problem need not use arrays)
0
 
override_y2kAuthor Commented:
They are arraylists and the spec i'm working with is arraylists.hashMaps would be more efficient but i'm just working to the spec i was given.
0
 
override_y2kAuthor Commented:
To SunBow, your commen was right.

I can see now that i'm initalising user to be null, so that everytinme it enters the loop it is set at null and therefore never gets into the else statement.Could you suggest a way to fix this, as if i just define User user; i will get initialization problems.

Thx for the help.

If anybody can correct that bug i will award the points!
0
 
override_y2kAuthor Commented:
Great thx for the help vk!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.