0

As i am new to java i got a task to find duplicate word only and its count. i stuck in a place and i am unable to get the appropriate output. I can not use any collections and built in tool. i tried the below code. Need some help, Please help me out.

public class RepeatedWord 
  {
   public static void main(String[] args) 
      {
          String sen = "hi hello hi good morning hello";
          String word[] = sen.split(" ");
          int count=0;
          for( int i=0;i<word.length;i++)
             {
                for( int j=0;i<word.length;j++)
                   {
                       if(word[i]==word[j])
                          {
                             count++;
                          }
                System.out.println("the word "+word[i]+" occured"+ count+" time");
                   }

             }

       }
 }
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
TTK
  • 47
  • 1
  • 7
  • Welcome to Stack Overflow! Questions should not be "moving targets." Your original code used `==`, and you got an answer correctly telling you not to use `==`. It's not appropriate at that point to change the question to using `equals` instead. If your code actually has the `==`, then you have your answer. If there is a separate, further problem, that's a separate question. – T.J. Crowder Sep 26 '15 at 12:36
  • @T.J.Crowder you marked it is duplicate.and you shared a link, i didnt find anything related to my question on that question. – TTK Sep 26 '15 at 12:39
  • Using `==` to compare strings is what's related. If you have a **different** bug in the code, revealed after fixing it, ask a question about that. – T.J. Crowder Sep 26 '15 at 12:43
  • @T.J.Crowder yes that was my silly mistake sorry for that. now after i changing it when i am trying to run code its showing only first element is duplicate not others. like i got 2 duplicates one *hi* and another *hello* – TTK Sep 26 '15 at 12:47
  • For the third time: Ask a new question. – T.J. Crowder Sep 26 '15 at 12:49

1 Answers1

0

Compare String with equals() not with ==

if(word[i].equals(word[j]))

== compares the refrence equals() compares the value of that object.

Apart from that second loop should have j<word.length

Change code to

String sen = "hi hello hi good morning hello";
String word[] = sen.split(" ");
int count = 0;
for (int i = 0; i < word.length; i++) {
    count = 0;
    for (int j = 0; j < word.length; j++) {

        if (word[i].equals(word[j])) {
            count++;
        }

    }
    if(count>1)   //show only duplicate word
    System.out.println("the word " + word[i] + " occured " + count + " time");
}

DEMO

singhakash
  • 7,891
  • 6
  • 31
  • 65
  • yes i did but still not getting output properly. – TTK Sep 26 '15 at 12:35
  • the word hi occured1 time the word hi occured1 time the word hi occured2 time the word hi occured2 time the word hi occured2 time the word hi occured2 time Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at RepeatedWord.main(RepeatedWord.java:13) – TTK Sep 26 '15 at 12:35
  • above out put i am getting – TTK Sep 26 '15 at 12:36
  • i tried the above code only its showing first elements count – TTK Sep 26 '15 at 12:44
  • @TTK you have problen in you second loop `for( int j=0;i – singhakash Sep 26 '15 at 12:45
  • yea i got it. but i am getting out put for each iteration. actually i want out put like below. *the word hello occured 2 time the word hi occured2 time* this two out put i want only – TTK Sep 26 '15 at 12:52
  • the duplicate word i want to print only with count – TTK Sep 26 '15 at 12:53
  • @TTK then just place if condition before print checking if count is greater than 1 check the link http://ideone.com/bSWeVT – singhakash Sep 26 '15 at 12:54
  • yea thanks. its showing the out put multiple time that the word hi occured 2 time the word hello occured 2 time can we print it only once not for repeated time. i think its printing multiple time for looping. can we ?? – TTK Sep 26 '15 at 13:29