-3

~~This is my code Can Anybody tell me why outputs are not coming correct?

class FrequencyOfWord
{
  public static void main(String dt[])
  {
    String str="Hello World Hello";
    int i=0,j=0,space=0,count=0;
    for(i=0;i<str.length()-1;i++)
    {
        if(str.charAt(i)==' ')
        {
            space++;
        }
    }
    String arr[]=new String[space+1];
    String cstr="";
    for(i=0;i<str.length();i++)
    {
        if(str.charAt(i)==' ')
        {
            arr[j]=cstr;
            cstr="";
            j++;
        }
        else
        {
            cstr=cstr+str.charAt(i);
        }
        arr[j]=cstr;
    }
    //System.out.println(str);
    for(i=0;i<arr.length;i++)
    {
        System.out.print(arr[i]);
    }
    for(i=0;i<arr.length;i++)
    {
        count=0;
        int flag=0;
        for(j=0;j<arr.length;j++)
        {
            if(arr[i]==arr[j])
            {
                count++;
            }
            if((arr[i]==arr[j])&&(i>j))
            {
                flag=1;
            }
        }
        if((count!=0)&&(flag==0))
        {
            System.out.println(arr[i]+"\t\t"+count);
        }
    }
  }
}

the output of count is coming to be 1 for each word. Can anyone tell me the error. The flag variable is used so that only once the frequency of a word is printed.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Please use debugger, SO cannot be used as a replacement for debugger. You can ask specific problem you are facing not basic debugging. Read Help Center. – Pradeep Simha Jun 29 '17 at 06:39
  • 1
    Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you still have issues afterwards, please feel free to come back with a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) that demonstrates your problem. – Joe C Jun 29 '17 at 06:39
  • `arr[i]==arr[j]` -> `arr[i].equals(arr[j])` – Eran Jun 29 '17 at 06:40
  • You appear to be reinventing `str.split(" ")`. – Andy Turner Jun 29 '17 at 06:40

1 Answers1

1

Your code is way way too complex - try using split and a HashMap

     String str="Hello World Hello";
     HashMap<String, Integer> res = new HashMap<String, Integer>();
     
     String el [] = str.split("\\s+");
     for (String s : el) {
         int count = 0;
         if (res.containsKey(s)) {
             count = res.get(s);
         }
         res.put(s, count + 1);
     }
     
     // output
     for (String keys : res.keySet()) {
         System.out.printf("%s : %d%n", keys, res.get(keys));
     }

output

World : 1

Hello : 2

EDIT: Or using Java 8 you can do

Map<String, Long> freq =
    Stream.of(str.trim().split("\\s+"))
          .collect(Collectors.groupBy(w -> w, Collectors.counting()));
freg.forEach((k, v) -> System.out.println(k + ": " + v);
Community
  • 1
  • 1
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64