0

I am working on a simple Java program where we have sorted string array named arr I am trying to compare two adjacent string and calculate frequency for each string present in that array

for(j1=0;j1<arr.length;j1++){
    if(j1+1 < arr.length){ // To prevent arrayOutofBoundsException
         if(arr[j1].equals(arr[j1+1])){
              counter++;
         }
         else {
              System.out.println(arr[j1]+" "+counter);
              counter=1;
         }

But it's not working right , what's wrong ? edit:problem is not in comparing , it's not calculating frequency as desired

10
  • 6
    What do you mean by "It's not working right"? And have you read How do I compare strings in Java? Commented Feb 14, 2014 at 13:15
  • 2
    And we still answer these questions Commented Feb 14, 2014 at 13:16
  • Fixed that problem but the thing is it's not giving desired output Commented Feb 14, 2014 at 13:18
  • @Sigma Use your debugger and try to see what's wrong with your logic. Commented Feb 14, 2014 at 13:19
  • @ZouZou I have been banging my head since yesterday but still i was't able to solve so asked you Commented Feb 14, 2014 at 13:20

4 Answers 4

2

OK, besides the equals fix, you want to keep the original order of words:

String orig = "hellow hello hello how how he ho" ;
//String orig = "how are you how do you do";

String[] arr = orig.split(" ");

//Arrays.sort(arr);

for(int j1 = 0; j1 < arr.length; j1++){
    if (arr[j1] != null) {
        int counter = 1;
        for(int j2 = j1+1; j2 < arr.length; j2++) {
            if(arr[j2] != null && arr[j1].equals(arr[j2])){
                counter++;
                arr[j2] = null;
            }
        }
        System.out.println(arr[j1]+" "+counter);
    }
}

The trick is that I run through the array, count all occurrences, null the occurrences, so they don't count again, and print the count. No need to sort the array.

Sign up to request clarification or add additional context in comments.

7 Comments

please check again , I am sorry for that mistake
@Sigma I found one bug in your code, please recheck.
Please provide some sample input and output.
@Sigma Are you splitting the input by whitespace?
@GaborSch any heads on this one?
|
0

== compares object identity in terms of memory address - to compare objects in terms of equality, use the equals-method.

Comments

0

This should work:

public static void main(String[] args)
    {       
        String text = "how are you how do you do";
        String[] keys = {"how", "are", "you", "how", "do", "you", "do"};
         Arrays.sort(keys);
        String[] uniqueKeys;
        int count = 0;
        System.out.println(text);
        uniqueKeys = getUniqueKeys(keys);

        for(String key: uniqueKeys)
        {
            if(null == key)
            {
                break;
            }           
            for(String s : keys)
            {
                if(key.equals(s))
                {
                    count++;
                }               
            }
            System.out.println("Count of ["+key+"] is : "+count);
            count=0;
        }
    }

    private static String[] getUniqueKeys(String[] keys)
    {
        String[] uniqueKeys = new String[keys.length];

        uniqueKeys[0] = keys[0];
        int uniqueKeyIndex = 1;
        boolean keyAlreadyExists = false;

        for(int i=1; i<keys.length ; i++)
        {
            for(int j=0; j<=uniqueKeyIndex; j++)
            {
                if(keys[i].equals(uniqueKeys[j]))
                {
                    keyAlreadyExists = true;
                }
            }           

            if(!keyAlreadyExists)
            {
                uniqueKeys[uniqueKeyIndex] = keys[i];
                uniqueKeyIndex++;               
            }
            keyAlreadyExists = false;
        }       
        return uniqueKeys;
    }

Output:

how are you how do you do
Count of [how] is : 2
Count of [are] is : 1
Count of [you] is : 2
Count of [do] is : 2

Comments

-1

Are you looking some sort of this

public static void main(String[] args){
        String[] arr = new String[5];
        arr[0] =  "One";
        arr[1] =  "Two";
        arr[2] =  "One";
        arr[3] =  "Three";
        arr[4] =  "Two";

        List<String> lstString = Arrays.asList(arr);
        Collections.sort(lstString);
        for(String eachString : arr){
            System.out.println("Frequency of " + eachString + " is " + getFrequency(eachString,lstString));
        }
    }

    private static int getFrequency(String word, List lstOfString){
        int frequency = 1;
        if(lstOfString != null && lstOfString.size() > 0){
            int firstIndex = lstOfString.indexOf(word);
            int lastIndex = lstOfString.lastIndexOf(word);
            frequency += lastIndex - firstIndex;
        }
        return frequency;
    }

Result : Frequency of One is 2 Frequency of One is 2 Frequency of Three is 1 Frequency of Two is 2 Frequency of Two is 2

1 Comment

Add the reason or explain properly what is your desire.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.