• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with file reading and array  RSS feed

 
Nathan D Jones
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have a simple assignment to complete. Need some help with some code. I have a file in the format:
userID1, museum
userID1, restaurant
userID2, museum
UserID1, restaurant

This file format cannot be changed. I have already read the file and stored each entry in the file in an array of userID and place type indicated by museum, restaurant, etc. What I would like to do is process this array then write a file in the following format:

UserID1, museum, 1
UserID1, restaurant, 2
UserID2, museum, 1
UserID2, restaurant, 0

I need help with the code to generate the format above or a suggestion to point me in the right direction. Thanks in advance.

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Get a pen and paper and write down, in detail (as if you were explaining it to a 5 year old), how you would do this if someone gave you the input data and told you to produce the desired output. Then, once you have fully described the solution, and only then think about converting that to code.
 
Nathan D Jones
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So I tried your approach and came back with this code, but now keywords count doesn't seem to be working well. Overview of code below: read a file name log1.txt, extract three pieces of information: userID, place and keywords. Write to an intermediary file. Read this very same file line by line and if the user ID equals a certain user, create a profile based on keywords. For example:

User1,place1,key1;key2;key3,lkjlkj,khkjkhj
Uer1,place,key1;key4,key5,hjghj,gjhg

Expected result:
User1, key1 2|key2 1|key3 1|key4 1|key5 1

Everything works up to the point where I believe the bug is. When I add the keywords to the array list KeyList. My keywords do not add properly. You can run this code and see the result. Simply create a log file named log1.txt with the format above. Any help, please. Spent hours looking at this and I might just be missing something, but the logic is good. Thank you.


 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, UseCodeTags <- click this

Why didn't you follow Tony's advice? Why didn't you think about the problem thoroughly before you coded?
Why do you write data to a file just to read it again in a moment? Can't you store your result in some sort of collection?

Why this?What if there is more data and it doesn't fit into this array?
You'll get ArrayIndexOutOfBoundsException.

Why are you using an array? I would use a Map.

Nathan D Jones wrote:but now keywords count doesn't seem to be working well.

ItDoesntWorkIsUseless <- click this

What do you mean by 'not working very well'?


Also thisYou could just write I didn't even try to use ++ operator because this line would be unreadable ;).
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I have added code tags, and they make the code look a lot better, but the indentation is inconsistent and you should use four spaces each time, not 2.
You have a dreadfully long method; the main method should be very short. If I see a method that long, I assume it is incorrect on spec.
I can see a few things: never use == true or == false. They are poor style and error‑prone if you write = by mistake. Not if (b == true) ... or if (b == false) ... but if (b) ... and if (!b) ...
Leave out the spaces in int [ ] [ ] Write int[][] ...
THEMATRIX is not a constant, and should therefore not be written in CAPITAL LETTERS.
I have no problems with using the ++ operator and assigning to an array in one statement, but i++ causes a lot of confusion, so you need to be very careful how to do it. And once you have worked out how to use ++ correctly, you always use it the same way.
If you use a Scanner to read the file, why not use its opposite to write a file?
You are making the mistake of trying to write everything and getting confused. If you had a file reading method, you could give names to all the tokens and print them out. That would verify that you are reading the file correctly. Then, after verification, you could remove the testing code and proceed with the next stage of the programming.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you changing delimiters on the Scanner?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Please, UseCodeTags <- click this

Why didn't you follow Tony's advice? Why didn't you think about the problem thoroughly before you coded?
Why do you write data to a file just to read it again in a moment? Can't you store your result in some sort of collection?

Why this?What if there is more data and it doesn't fit into this array?
You'll get ArrayIndexOutOfBoundsException.

Why are you using an array? I would use a Map. (...)

I cannot call a reply like this one anything other than "Bullying a Beginner". Do you really think you
are helping the OP here?

Remember that this forum is for beginners, and that this site is a friendly site.

Greetings,
Piet
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:I cannot call a reply like this one anything other than "Bullying a Beginner". Do you really think you
are helping the OP here?

I didn't mean to bully anyone. And yes, I think I am helping OP. I realized a long time ago that asking someone "Why did you do X?" helps that person to find out why X was a bad idea. If a person really thought about it.
Sorry if my post was a little harsh. English is not my 1st language (nah! it isn't event 2nd ) and I might not realized that.

Next time I will write "Please, don't be offended. Please, tell me what is a reason you used array in a place where -- in my humble opinion -- a Map would do better."
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted and let's return to the official theme of the thread.
 
Nathan D Jones
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the great feedback. Do recognize there as some efficiency gaps. Let me answer some questions:

1: What if there is more data and it doesn't fit into this array? This a test model. With this implementation there is zero chance of the data exceeding this array size for this project.
2. Why change delimiter? The file structure that I am reading has the following format: user1,place,key1;key1;key,etc,etc,
3. Why read then write then read again? Requirement of the project.
4. Why not use a map? Beginner, not familiar with this data type
5. Not taking this personally so don't worry about harsh responses. I put it out there for review so understand that feedback might not be pretty.
6. What does it mean by not working? For the user above, based on the code, the user profile should result in user1, key 1|key1 2. The code is generating some other result. Keywords are being added properly, but not being added to the keyLisy arrayList the way that I would expect. The system out at line 106 indicates that keywords are loading correctly. Lines 111 through to line 123 is the logic to add keywords to the arrayList and what to do if the keywords already exists in the keyList arrayList

Thanks again.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan D Jones wrote:Thanks for all the great feedback. . . .
You're welcome
1: What if there is more data and it doesn't fit into this array? . . .
Two answers:
  • 1: Cross that bridge when you get to it. Get the program working with your small file and worry about large files afterwards.
  • 2: Use a List. You appear to be familiar with ArrayList already.
  • 2. Why change delimiter? . . .
    But you appear to be changing the delimiter after reading. Decide which delimiter you require and set it at the start of the program.
    3. Why read then write then read again? Requirement of the project.
    OK
    4. Why not use a map? Beginner, not familiar with this data type
    Maps are really good; you can read about them here. I have not read enough of your code to know whether a Map will be useful in your program or not.
    5. Not taking this personally so don't worry about harsh responses. . . .
    Thank you.
    6. What does it mean by not working? For the user above, . . .
    You can only sort that sort of problem out in small stages. Have you got a class which encapsulates all those data? Can you put the information into it? If you can get the info into the class, you can create an object, then you can get the info back from it with its methods.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!