Page 1 of 2 12 LastLast
Results 1 to 20 of 27
Like Tree1Likes

Thread: Help to find solution plz.

  1. #1
    hell00 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Help to find solution plz.

    I need to read strings from file. Write only those strings that have duplicates and number of duplicates for them to another file. How can i do it?
    Last edited by hell00; 12-18-2011 at 10:38 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help to find solution plz.

    Quote Originally Posted by hell00 View Post
    I need to read strings from file. Write only those strings that have duplicates and number of duplicates for them to another file. How can i do it?
    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.

  3. #3
    hell00 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default 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?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default 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

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help to find solution plz.

    Do i have to take every string and then compare it with the rest in cycle ?
    Whatever you do has to be described more precisely than this. So, you compare every string with every other. So what? How does that help you find the duplicates and their frequencies?

    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.

  6. #6
    hell00 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default 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;
    }
    sortList is ArrayList which already contains strings from file. I do a cycle for every string from list comparing it with the rest and if i find duplicate i increment p. After the cycle is complete if there were duplicates i try to write this string to file and number of duplicates. I write name of the file in command line. It creates the file but it is empty. Why?
    Last edited by Norm; 12-18-2011 at 11:21 PM. Reason: added code tags

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default Re: Help to find solution plz.

    It creates the file but it is empty. Why?
    Did you close the file after you finished writing to it?

  8. #8
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Help to find solution plz.

    If your file contained just 4 strings all the same, what would p end up as?

  9. #9
    hell00 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default 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.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default Re: Help to find solution plz.

    it gives me error.
    Please post the full text of the 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.
    Fubarable likes this.

  11. #11
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Help to find solution plz.

    Quote Originally Posted by hell00 View Post
    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.
    That doesn't sound like the correct solution?

  12. #12
    hell00 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default 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.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default Re: Help to find solution plz.

    Any suggestions plz?
    Try working out your logic with a piece of paper and a pencil like suggested in post#5.
    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.

  14. #14
    hell00 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default 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-2
    Last edited by hell00; 12-18-2011 at 11:53 PM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default 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.

  16. #16
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Help to find solution plz.

    When you remove something from a List, does it leave a hole?

  17. #17
    hell00 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: Help to find solution plz.

    Yeah i think that is the reason. Thanks!

  18. #18
    hell00 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default 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?

  19. #19
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Help to find solution plz.

    A null where? I thought you said there was no hole?

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,415
    Rep Power
    25

    Default Re: Help to find solution plz.

    Go back to your piece of paper and look at the value of the indexes being used to go through the list.
    You can change their values inside of the loops.

    Think about what the for loop is going to do to the indexes and when it does it.

Page 1 of 2 12 LastLast

Similar Threads

  1. I need solution for this program
    By magesh5 in forum New To Java
    Replies: 3
    Last Post: 01-11-2011, 01:53 PM
  2. Comments on my FPS solution
    By trader5050 in forum New To Java
    Replies: 1
    Last Post: 11-17-2010, 04:51 PM
  3. Please I need the solution to this
    By debobbt in forum New To Java
    Replies: 4
    Last Post: 12-18-2009, 04:34 AM
  4. solution for my project
    By shkelqa in forum AWT / Swing
    Replies: 4
    Last Post: 05-28-2008, 10:31 PM
  5. Please need solution
    By prithvi in forum New To Java
    Replies: 4
    Last Post: 04-22-2008, 01:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •