Solved

Problems when parsing file

Posted on 2003-12-01
7
383 Views
Last Modified: 2010-04-17
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++;
        }
0
Comment
Question by:override_y2k
  • 4
  • 2
7 Comments
 
LVL 2

Author Comment

by:override_y2k
ID: 9850466
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
 
LVL 24

Expert Comment

by:SunBow
ID: 9850682
Sorry, I don't code that way, but you might recheck this sequence:

     User user = null;
...
            user = tmp;
             break;
 ...
       if (user == null) {
 
0
 
LVL 24

Expert Comment

by:SunBow
ID: 9850757
(this problem need not use arrays)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:override_y2k
ID: 9851965
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
 
LVL 2

Author Comment

by:override_y2k
ID: 9852324
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
 
LVL 4

Accepted Solution

by:
vk33 earned 100 total points
ID: 9859176
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
 
LVL 2

Author Comment

by:override_y2k
ID: 9860432
Great thx for the help vk!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question