0

Please help me writing a code using string (or StringBuffer) in java. Pls don't use any other datastructure rather than String or StringBuffer. I got the answer but the code counts the characters again from starting till it has 0 characters. o/p is given at the end pls check it. Thanks in advance.

    import java.util.*;

public class DuplicateWordsOcc {
    static Scanner sc = new Scanner(System.in);
    int i,j,k;
    void show(String s,char ch,int n){
        for(i=0;i<s.indexOf(ch);i++){
          if(s.charAt(i)!=ch){
            System.out.println(ch+" : "+n);
          }
        }
    }

    public static void main(String args[]){
        DuplicateWordsOcc ob1 =new DuplicateWordsOcc();
        System.out.println("Enter the string: ");
        String s1 = sc.nextLine();
        String s2 = new String(s1);
            int i,j,k;
            for(i=0;i<s1.length();i++){
                    int count=1;
            for(j=i+1;j<s1.length();j++){
                if(s1.charAt(i)==s1.charAt(j)){
                    count++;
                    ob1.show(s1,s1.charAt(i),count);
                }
            }
        }
    }
}

Output

Enter the string:

Hello

l : 2

Here is another Output

Output

Enter the string:

Hello Hello Hello Hello

l : 2

l : 3

l : 4

l : 5

l : 6

l : 7

l : 8

l : 2

l : 3

l : 4

l : 5

l : 6

l : 7

o : 2

o : 2

o : 2

o : 3

o : 3

o : 3

o : 4

o : 4

o : 4

: 2

: 2

: 2

: 2

: 3

: 3

: 3

: 3

l : 2

l : 3

l : 4

l : 5

l : 6

l : 2

l : 3

l : 4

l : 5

o : 2

o : 2

o : 2

o : 3

o : 3

o : 3

: 2

: 2

: 2

: 2

l : 2

l : 3

l : 4

l : 2

l : 3

o : 2

o : 2

o : 2

l : 2

In the Second output pls note the letters in bold that is the real answer which shows in the very first iteration.I want to stop the execution after getting that.

From the 1st output i came to conclusion that more than count 2 starts making problems.

I know the problem is at the for loop inside show() method pls correct my code.

Pardon my English.

7
  • 1
    A) Try and use full English words, not shorthand that can be confusing to people. B) Try and give a minimal example. It's not at all clear what all that stuff relates to. Commented Feb 27, 2018 at 19:03
  • Thanks sir for your response,I will follow A part.But inorder to demonstrate the full working and to get desired answer i need to go for this large output.please read the paragraph after output and check my second output you will get what I said.Thanks for the tip Sir. Commented Feb 27, 2018 at 19:13
  • You can always omit parts of the output that are replicated, or you can at least delete the blank lines by formatting that as code. Commented Feb 27, 2018 at 19:14
  • Its not replicated Sir,Its system generated.I didn't edit my output just highlighted that i got the desired output but it keeps on counting after that.To demonstrate the latter problem I have to put all the output.For the former part I highlighted the output.Sir please tell your friends who are good in java to give solution.I think your main stream is Ruby.Thanks in advance. Commented Feb 27, 2018 at 19:20
  • By formatting the output in the form of code I can't highlight the important parts in the output, which is the main part because by inferring that others can see that I got the output already but not in the way i needed. If I put one line break the compiler didn't recognise it and prints everything in same line,But if I put two line breaks it skips one line.Again Thanks for the tip Sir I will try to improve like what u said from next time onwards .I am pretty new to this forum.Please tell some one to address my question. Commented Feb 27, 2018 at 19:36

1 Answer 1

2

Here is the modificated code:

import java.util.*;

public class DuplicateWordsOcc {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args){
        System.out.println("Enter the string: ");
        String s1 = sc.nextLine();
        s1 = s1.replaceAll("\\s","").toUpperCase();
        int len = s1.length();
        while(len > 0) {
         int count = 1;            
         for(int j=1;j<len;j++){
            if(s1.charAt(0)==s1.charAt(j)){
                count++;
            }
         }
         if (count > 1) {
            System.out.println(s1.charAt(0)+" : "+count);    
         }

         String character = String.valueOf(s1.charAt(0)).trim();
         s1 = s1.replaceAll(character,"");
         len -= count;

       }
    }
}

Output from "He is a very good athlete and he can swim." is

H : 3
E : 5
I : 2
S : 2
A : 4
O : 2
D : 2
T : 2
N : 2
Sign up to request clarification or add additional context in comments.

3 Comments

@ArunHC I've updated my code because when I do replacing then string length is changing every time and "o" character from my example was never counted. now every character is counted and only duplicates are shown.
It works for "Hello Hello" and "Sniper killer" here both sentence have same no of words (i.e. first one got two 5 letter words and second got two 6 letter words). Please check.
Sir it is not working if I use different length words in my sentence like "He is a very good athlete and he can swim.". Please check.

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.