finding two matching strings
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: finding two matching strings

  1. #1
    Join Date
    Aug 2006
    Posts
    144

    finding two matching strings

    Hello,

    I have four strings, string1, string2, string3 and string4. Two of these strings are equal, now I want to find out which are equal and put the result into two strings, e.g. result1="3" result2="4" if string 3 and 4 are equal. Of course I can do an if then else ladder

    Code:
    if(string1.equalsIgnoreCase(string2))
        		{
        			result1="1";
                                                    result2="2";
        		}
    else if(string1.equalsIgnoreCase(string3))
        		{
        			result1="1";
                                                    result2="3";
    
        		}
    //and so on
    Is there a better way of doing this? result1 can be given as a string as above or as well as an integer if that makes it easier.

    Having 4 strings the above code can handle it, but what if I have a 100 strings.

    Thanks,
    J.

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: finding two matching strings

    Yes, there is a better way. You could be using either a String array, or an ArrayList of type String. Then you could be comparing the values in a loop (or multiple loops) and assigning the value of the result in there with only having one or two conditional if statements. This would allow you to have any number of strings that are comparable.

  3. #3
    Join Date
    Jan 2011
    Posts
    2

    Re: finding two matching strings

    May be we can convert the given strings to an integer using a hash function and compare only those strings whose hash values are same. Thinking in these lines may help reach an optimal solution

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: finding two matching strings

    You could use a HashSet and add one string at a time. Before you add each string call the set's contains() method to see if it is already there. If it is you've found your match, if not you add the string and try the next one.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jan 2011
    Posts
    2

    Re: finding two matching strings

    @keang: Yes, that is the best approach.

    @joebar: Here is some working code

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;

    public class CountFrequency {

    /**
    * The purpose of this class is to find the frequency of each string in a text document. As of now, the text file is
    * restricted to one line
    *
    * @param args
    * @throws IOException
    */
    public static void main(String[] args) throws IOException {

    try {
    FileReader fr = new FileReader("D:/textDoc.txt");
    BufferedReader br = new BufferedReader(fr);
    String[] line = br.readLine().split("[ ,\\.]");

    /*
    * Put string and frequency as key-value pairs in a hash map
    */
    Map map = null;
    for (String s : line) {
    if (s != null && s.trim()!="") {
    if (map == null) {
    map = new HashMap();
    map.put(s, 1);
    } else {
    int value = map.containsKey(s) ? ((Integer) map.get(s) + 1) : 1;
    map.put(s, value);
    }
    }
    }

    Set set = map.keySet();
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
    String s = (String) iter.next();
    System.out.println("String: " + s + "\t frequency: " + map.get(s));
    }

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }

    }

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center