0
package Collections;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;

public class Stringchar {

    public static void main(String[] args) {

        int count =0;
        String s = "mmamma";

        //System.out.println(s.length());

        LinkedHashSet<Character> ch = new LinkedHashSet<Character>(); 
        for (int i=0; i<s.length(); i++){
            ch.add(s.charAt(i));
        }

        Iterator<Character> iterator = ch.iterator();
        while(iterator.hasNext()){

            Character st = (Character) iterator.next();
            for (int k=0; k<s.length() ; k++){
                if(charAt(k)==  st){ // Why this charAt method is not working?   
                    count = count+1;
                }

                if(count>1) {
                    System.out.println("Occurance of "+ st + "is" + count);
                }
            }                           
        }           
    }
}

I am new to coding so I might be silly in asking this question. I have written a code where I am trying to print the occurrences and the number of the same of one character in a string using sets however I am facing some issues in doing so. Request you to help.

1
  • Just for the record, as newbies often forget about that: please consider accepting one of the answers at some point. Commented Sep 10, 2018 at 14:32

5 Answers 5

1

Here:

charAt(k);

is basically the same as

this.charAt(k);

In other words: you are trying to invoke a method on the class this code sits in.

I assume you intended to do someStringVariable.charAt(k) instead! ( sure, you meant s.charAt(), but s is a terrible, nothing telling name for a variable. Your variables are your pets, give them names that mean something!)

Sign up to request clarification or add additional context in comments.

1 Comment

Yes sure. I will take care of this in future.
0

The method charAt is not static and need to be applied on a given String, if not how to know where to look for the xxth char ?

str.charAt(index);

Also, the print operation would better be after the for loop which counts the occurences, if not you'll have a print at each occurence

for (int k=0; k<s.length() ; k++){
    if(s.charAt(k) ==  st){    
        count = count+1;
    }
}
if(count>1) {
    System.out.println("Occurance of "+ st + "is" + count);
}

2 Comments

Thanks.. I corrected this and now I am getting correct results.
@UjjwalSharma ok so don't hesite to vote up the answers that helped you, and accept one ;)
0

I suppose you want to check, how often the Character appears in your string (String s = "mmamma";).

The charAt() method has to be applied on a String object, so you have to change the if condition from this:

if(charAt(k) ==  st)

To this:

if(s.charAt(k) ==  st)

Comments

0

The problem is that you are trying to get a character at a position of a character. When you create the variable st it is a character and will have a length of 1; there fore you are unable to get a charAt(index) there. Additionally this method of using the LinkedHashSet will not work because when you add those characters to the LinkedHashSet it will not add each character more than once. Instead you want an ArrayList.

This is probably not the most efficient solution but it will accomplish what you are trying to do with the HashSet

String s = "mmamma";

List<Character> characterList = new ArrayList<>();
LinkedHashSet<Character> characterLinkedHashSet = new LinkedHashSet<>();

for(char c : s.toCharArray()) {
    characterLinkedHashSet.add(c);
    characterList.add(c);
}

for (Character character : characterLinkedHashSet) {
    int frequency = Collections.frequency(characterList, character);
    System.out.println("The frequency of char " + character + " is " + frequency);
}     

So what this does it is creates your LinkedHashSet as well as an ArrayList. The ArrayList stores all of the characters in a Collection and the LinkedHashSet stores only one instance of each Character. We can then loop over the HashSet and get the frequency inside the ArrayList

Comments

0

You have to correct your code like so,

while (iterator.hasNext()) {
    int count = 0;

    Character st = (Character) iterator.next();
    for (int k = 0; k < s.length(); k++) {
        if (s.charAt(k) == st) { // Why this charAt method is not working?
            count++;
        }
    }
    if (count > 1) {
        System.out.println("Occurance of " + st + " is: " + count);
    }
}

charAt method is available in String class hence you have to call it on a String reference. I have made few more improvements to the code too. Declare the count variable inside the while loop which is less error prone. Finally notice that I have moved the if statement away from the for loop since it gives some spurious intermediary results if it is kept inside the for loop.

Comments

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.