Results 1 to 20 of 27
Thread: Help to find solution plz.
- 12-18-2011, 10:34 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
-
Re: Help to find solution plz.
Myself, I'd use Java. As for more specific help, you'll need to ask a more specific question. What steps can you do, and what steps exactly are you stuck on? If it's all steps, then you'll want to check out the Java tutorial which you can find here: The Really Big Index.
Much luck.
- 12-18-2011, 10:48 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
Re: Help to find solution plz.
I am stuck at finding duplicates and counting there numbers. Do i have to take every string and then compare it with the rest in cycle ? I didnt get any results doing this. Is there a better way?
- 12-18-2011, 10:56 PM #4
Re: Help to find solution plz.
Do you know about the collections classes?
The Set class would be useful.
For much more info, go to this site, Find Collections and read up.
http://docs.oracle.com/javase/tutori...ybigindex.html
- 12-18-2011, 11:03 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Help to find solution plz.
Do i have to take every string and then compare it with the rest in cycle ?
The thing to do is to imagine how you (not a computer) would do this task. Imagine yourself looking at this web page and, using a blank sheet of paper and a pencil, determining which words are duplicated and how many times each appears. The task itself is *not* difficult: quite literally a child could do it. What *is* difficult is expressing what you would do comprehensively and precisely. But you must do that to have any chance of writing valid computer code.
-----
A couple of things strike me about the problem, and how I would attempt it. First, as I look at each word on the page I cannot be sure that it won't be a duplicate until I get to the very end. So I am going to have to keep track of each word in case I see it again. Secondly, the problem asks for the frequencies of the duplicates, so I figure I would have to keep track of this as well.
- 12-18-2011, 11:03 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
Re: Help to find solution plz.
Yes, I know about them. Here is what i wrote:
Java Code:for (int i = 0; i < sortList.size(); i++) { for (int j = i+1; j < sortList.size(); j++) { if (sortList.get(i).equals(sortList.get(j))) { p=p+1; } } if (p!=0) { try{ PrintWriter pw = new PrintWriter(new FileWriter(args[1], true)); pw.println(sortList.get(i)+"="+p); } catch(Exception e){} } p=0; }
Last edited by Norm; 12-18-2011 at 11:21 PM. Reason: added code tags
- 12-18-2011, 11:15 PM #7
Re: Help to find solution plz.
It creates the file but it is empty. Why?
- 12-18-2011, 11:19 PM #8
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Help to find solution plz.
If your file contained just 4 strings all the same, what would p end up as?
- 12-18-2011, 11:27 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
Re: Help to find solution plz.
No i didnt close the file. I have a problem with that - no matter where i put pw.close() it gives me error. Could you plz advice me where to put it?
to 2by4:
if i have 4 same strings p will be 3 for the first string, 2 for second, 1 for third, and 0 for last one.
- 12-18-2011, 11:30 PM #10
Re: Help to find solution plz.
it gives me error.
Why do you create the PrintWriter object inside of the loops?
It would be better (and faster) to create it once outside the loops.
- 12-18-2011, 11:33 PM #11
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
- 12-18-2011, 11:42 PM #12
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
Re: Help to find solution plz.
Yes it is not correct. Because i only need number of duplicates for the first string which is 3. I add a line to remove a string if it is a duplicate. But for some reason it is not working properly
for (int i = 0; i < sortList.size(); i++) {
for (int j = i+1; j < sortList.size(); j++) {
if (sortList.get(i).equals(sortList.get(j))) { p=p+1;
sortList.remove(j);
}
}
if (p!=0) { System.out.println(sortList.get(i)+" "+p);
try{
PrintWriter pw = new PrintWriter(new FileWriter(args[1], true));
pw.println(sortList.get(i)+"-"+p);
pw.close();
} catch(Exception e){}
}
p=0;
}
Here it should delete duplicates so they won't be used in a cycle. But the count of duplicates is always one less that it should be. It also for some reason dont remove all duplicates. Here is example:
input
12
12
12
12
12
32
32
32
11
11
output 12-2, 12-1, 32-1, 11-1
when it should be 12-4, 32-2, 11-2
Any suggestions plz?
pw.close() now seems to work fine. But it adds lines to existing file instead of rewriting it.Last edited by hell00; 12-18-2011 at 11:46 PM.
- 12-18-2011, 11:46 PM #13
Re: Help to find solution plz.
Any suggestions plz?
Get some playing cards or other easy to handle items, put them in a pile and count the duplicates.
Think about the steps you took not to count the same duplicate item more than once. When you get the logic worked out, then try writing the code.
Design first, code later.
A problem you might be having is removing items changes the index value for the following items.Last edited by Norm; 12-18-2011 at 11:50 PM.
- 12-18-2011, 11:49 PM #14
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
Re: Help to find solution plz.
I already did that and this code is the result of it :)
Btw if i change this line:
for (int j = i+1; j < sortList.size(); j++)
into:
for (int j = i; j < sortList.size(); j++)
it gives almost correct answer 12-3, 32-2, 11-2Last edited by hell00; 12-18-2011 at 11:53 PM.
- 12-18-2011, 11:52 PM #15
Re: Help to find solution plz.
You might have missed my late addition:
A problem you might be having is removing items changes the index value for the following items.
- 12-18-2011, 11:57 PM #16
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Help to find solution plz.
When you remove something from a List, does it leave a hole?
- 12-18-2011, 11:57 PM #17
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
Re: Help to find solution plz.
Yeah i think that is the reason. Thanks!
- 12-19-2011, 12:00 AM #18
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
Re: Help to find solution plz.
Yeah i think that is the reason. Thanks!
to 2by4:
Looks like it does not leave a hole. Otherwise it would be working properly. I guess i have to write null there instead of deleting it. Do u know how to do it?
- 12-19-2011, 12:06 AM #19
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Help to find solution plz.
A null where? I thought you said there was no hole?
- 12-19-2011, 12:07 AM #20
Similar Threads
-
I need solution for this program
By magesh5 in forum New To JavaReplies: 3Last Post: 01-11-2011, 01:53 PM -
Comments on my FPS solution
By trader5050 in forum New To JavaReplies: 1Last Post: 11-17-2010, 04:51 PM -
Please I need the solution to this
By debobbt in forum New To JavaReplies: 4Last Post: 12-18-2009, 04:34 AM -
solution for my project
By shkelqa in forum AWT / SwingReplies: 4Last Post: 05-28-2008, 10:31 PM -
Please need solution
By prithvi in forum New To JavaReplies: 4Last Post: 04-22-2008, 01:27 PM
Bookmarks