Solved

Problems when parsing file

Posted on 2003-12-01
7
382 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scripting vs. Programming languages 25 165
tidtcpserver connection lost handle 2 83
numbers ascending pyramid 101 194
Adoquery sql  left join does not work 25 89
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

776 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