-4

Please help me with the logic for finding the count of words in String Array. Without use of String methods would be a plus point.

The code I have been working on:

class Scratch
{
    public static void main(String args[])
    {
        String[] str = { "sup", "aisu", "sup", "aisu", "sandy",
            "sandu", "vijay", "gani", "sup", "sup", "gani", "sandu" };
        int i = 0, j = 0, k = 0, count = 0;
        System.out.println("The array size is" + str.length);
        System.out.println("The array elements are");
        for (i = 0; i < str.length; i++)
            System.out.println(str[i]);

        System.out.println("Comparison");
        for (j = 0; j < str.length; j++)
        {
            for (k = 1; k < str.length - 1; k++)
            {
                if (str[j] == str[k])
                {
                    count++;
                    break;
                }
            }
        }
        System.out.println("," + count);
    }
}

Please note the logic should not contain Collections concept.

OUTPUT REQUIREMENT

Count of sup   is : 4
Count of aisu  is : 2
Count of sandy is : 1
Count of sandu is : 2
Count of vijay is : 1
Count of gani  is : 2
gknicker
  • 5,509
  • 2
  • 25
  • 41
supreeth v
  • 19
  • 8
  • What do you mean by "without String functions"? How can we check content of string without any of its functions (I am assuming you mean methods)? Also what do you mean by "Count the number of words"? What should be result for your example? – Pshemo Mar 06 '15 at 20:48
  • You mean you need to count unique words? – esin88 Mar 06 '15 at 20:48
  • 1
    You are already using `array.length`, what else you want? – Aninda Bhattacharyya Mar 06 '15 at 20:49
  • Pshemo , esin88 : Thanks for the reply . I need the output as for example my array ={"a","b","a"} , count of a : 2 , count of b : 1 – supreeth v Mar 06 '15 at 20:50
  • what do you want to do exactly? what is wrong in what you get ? – adrCoder Mar 06 '15 at 20:50
  • 1
    `str[j]==str[k]` won't do what you think it does. See [how to compare strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – azurefrog Mar 06 '15 at 20:52
  • @azurefrog in this case we are dealing with literals so they are interned which means `==` will work. But yes, this can lead to problems later when we will get strings dynamically so we should always use `equals`. – Pshemo Mar 06 '15 at 20:53
  • @Pshemo I know, but it won't work for the reason the OP thinks it does. I didn't intend to suggest an answer, just further reading. – azurefrog Mar 06 '15 at 20:54
  • @azurefrog OK, fair enough :) – Pshemo Mar 06 '15 at 20:54
  • @pbabcdefp : No Collections .. Only Arrays and String – supreeth v Mar 06 '15 at 20:59
  • @azurefrog,thanks for your reply... I have edited the question too and also was able to find the answer for what i was looking for . – supreeth v Mar 13 '15 at 20:08
  • @ Hovercraft Full Of Eels : I have edited the question too and also was able to find the answer for what i was looking for . – – supreeth v Mar 13 '15 at 20:09

3 Answers3

0

Here is your code how it should be, in one way...

public static void main(String[] args){

    String[] str ={"sup","aisu", "sup","aisu","sandy","sandu","vijay","gani","sup","sup","gani","sandu"};
    String[] alreadyValidated = new String[str.length];
      int i=0,j=0,count=0;
      System.out.println("The array size is"+str.length);
      System.out.println("The array elements are");
      for( i=0;i<str.length;i++)
                for(j=0;j<str.length;j++)
      {
            boolean skipCheck = false;
            for (String string : alreadyValidated) {
                if(str[i] == string) {
                    skipCheck = true;
                    break;
                }
            }
            if(!skipCheck) {
                System.out.println("Checking for word..."+str[i]);
                for(j=1;j<str.length-1;j++)
                {
                     if(str[i]==str[j])
                     {
                         count++;
                      }
                }
                System.out.println("The count is ..."+count+ " for word..."+str[i]);
                count = 0;
                alreadyValidated[i] = str[i];
            }
       }

}
  • What alteration has to be done if I want the output as count of sup : 4 and so on so forth – supreeth v Mar 06 '15 at 21:06
  • Sorry, I am not clear with the question. Can you please elaborate your requirement? – Aninda Bhattacharyya Mar 06 '15 at 21:08
  • @ Aninda Bhattacharyya : My requirement is to find the count of the words present in the String array . For instance I have an array a[]={"a","a","b","c"}; I want to get the result as count of a : 2 , count of b : 1 , count of c: 1. I want this result without using collection and if possible no string methods or minimal use of string methods – supreeth v Mar 06 '15 at 21:16
  • You just have to change the last ` System.out.println("The count is ..."+count+ " for word..."+str[i]);` for that. Still need help? – Aninda Bhattacharyya Mar 06 '15 at 21:18
  • the output im getting is count of sup : 3 this is printing three time ,count of aisu : 2 is repeating 3 two times but my requirement is count of sup : 4 ,count of aisu : 2 – supreeth v Mar 06 '15 at 21:32
  • Edited my answer, please check now. – Aninda Bhattacharyya Mar 06 '15 at 22:08
  • Yeah this is what I wanted to display thank you Aninda for sharing the knowledge and guiding me – supreeth v Mar 06 '15 at 22:18
0

You can simply check if the word exists in the array before the checked element:

String[] a = ...;
int count = 0;
for (int i = 0; i < a.length; i++) {
    boolean existsBefore = false;
    for (int j = 0; j != i; j++) {
        if (a[i].equals(a[j])) {
            existsBefore = true;
            break;
        }
    }
    if (!existsBefore) {
        count++;
    }
}
Bubletan
  • 3,833
  • 6
  • 25
  • 33
0
for (j = 0; j < str.length - 1; j++) {
  for (k = 1; k < str.length; k++) { 
    if (str[j] == str[k]) { 
      count++; 
    }
  }
  System.out.println("Num of words " + str[j] + ":" + count);
}
falsarella
  • 12,217
  • 9
  • 69
  • 115
Shiva Krishna
  • 193
  • 1
  • 2
  • 7