Question

My code is basically to find the number of words in a string (Yeah, really simple, but it isnt working). I've tried multiple methods and tried altering the code quite a few times. Can anyone explain to my why this code doesnt work and if it's possible to give me a better/working code.

 StringTokenizer stspace = new StringTokenizer(sent, " .?");
    int nTokenSpace = stspace.countTokens();



    String mspace[] = new String[nTokenSpace];


    for(int i=0;i<nTokenSpace;i++)
    {
        mspace[i] = stspace.nextToken();


    }
  for(int i=0;i<nToken;i++)
    {
       String k = mspace[i];
       int freq = 0;
       for(int x=0;x<nTokenSpace;x++)
       {
           if(k==mspace[x])
           {
               freq++;
            }
        }
        System.out.println(k+"\t"+freq);
    }

The Required output for "My. Name. Is. Anon. Is. Anon." should be

My  1
Name 1
Is 2
Anon 2

But I get 
My  1
Name    1
Is  1
Anon    1
Is  1
Anon    1

I have a feeling there's an obvious mistake here but I just dont see it (need new eyes >.>)

Was it helpful?

Solution

Your error is here:

if(k==mspace[x])

You need to use

if (k.equals(mspace[x]))

to compare Strings.

If you don't want the duplicates in the output, you can add your tokens to a Set before the loop:

Set<String> uniqueTokens = new LinkedHashSet<String>(Arrays.asList(mspace));
for (String k : uniqueTokens) {
    int freq = 0;
    for (int x = 0; x < nTokenSpace; x++) {
        if (k.equals(mspace[x])) {
            freq++;
        }
    }
    System.out.println(k + "\t" + freq);
}

OTHER TIPS

Comparison of Strings

replace

 if(k==mspace[x])
       {
           freq++;
        }

with

 if(k.equals(mspace[x]))
       {
           freq++;
        }

You have this line:

if(k==mspace[x])

This means that you compare two objects and they have to be exactly the same reference in memory, not identical. What you're looking for is two identical objects. To do that, you have to compare objects with .equals(Object).

if (k.equals(mspace[x]))

Just to add on to the other answers here, I believe you also need to remove duplicates from the list or your results will be be like:

My  1
Name    1
Is  2
Anon    2
Is  2
Anon    2

so your if-block should be:

for(int i=0;i<nToken;i++)
{

   String k = mspace[i];

   if (k.equals(""))
       continue;

   int freq = 0;
   for(int x=0;x<nTokenSpace;x++)
   {
       if(k.equals(mspace[x]) )
       {
             freq++;
             mspace[x] = "";
        }
    }
    System.out.println(k+"\t"+freq);
}

Why don't you use collection classes. Here is the code.

import java.io.*;
import java.util.*;
public class HelloWorld{

     public static void main(String []args)
     {
        String givenstring="My. Name. Is. Anon. Is. Anon.";
         String[] words=givenstring.split(" ");

         ArrayList<String> arr=new ArrayList<String>();
         for(int i=0;i<words.length;i++)
          arr.add(words[i]);

        while(arr.size()!=0)
         {

             String word=arr.get(0);
             int frequency=Collections.frequency(arr,word);
             arr.removeAll(Collections.singleton(word));
             System.out.println(word+frequency);
          }
     }
}

Hope it helps..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top