简体   繁体   中英

Check how many times a character occurs consecutively in a String

I am trying to count the number of times a character occurred consecutively in a String. Can't seem to go anywhere with this :\\ I would be highly appreciative if ye can help me out.

Thanks in advance :)

        int totCharacters=0, vowels=0, digits=0, odds=0, consecutive=0, index=0;
        String text;
        char ch;

        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a sentence terminated with a full stop or exclamation mark: ");
        text = input.nextLine();

        do {
            ch = text.charAt(index);

            if(ch=='.' && ch=='!')
                break;

            if(index<text.length()-1)
                totCharacters++;
            if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
                vowels++;
            if(ch>='0' && ch<='9')
                digits++;
            if(ch=='1' || ch=='3' || ch=='5' || ch=='7' || ch=='9')
                odds++;

            index++;

        }while(ch!= '.' && ch!='!');

        for(int i=0; i<text.length(); i++)
        {

            if(ch==text.charAt(i))
                consecutive++;

        }

I'll assume the problem is that it is not counting consecutive the way you expect. The problem is that the for loop is running after you've gone through all the characters. Since ch is set the last time when a '.' or '!' is encountered, the for loop is just counting all the '.' or '!' characters. And not just the consecutive ones.

I would do something like this. Have two string, one holding all consonants, the other holding all the vowels. Check if one of the strings contains the character. If not, then it is a punctuation or space. The code filters out the space.

String consString = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
String vowelString = "aeiouAEIOU";

for (int i = 0; i < s.length(); i++){
    if (consString.contains(s.charAt(i)){
        consCount++;
    }
    else if (vowelString.contains(s.charAt(i)){
        vowelCount++;
    } 
    else if (!Character.isWhiteSpace(s.charAt(i))
        punctCount++;
    }
}

You can do this for any character set you want. Also if you want to count consecutives, you can keep a currentChar variable and check if the next variable equals it. If it does, consecutives++

This will count ALL consecutive chars ( i believe, can't compile atm). This is probably not what you want. I can modify depending on what you mean by consecutive characters. ALL consecutive? highest consecutive?

I moved the for loop inside the do while so that you do it to each char. I also base the int i variable off of index so that you don't look at characters before the current one you are examining. Also moved the incrementing of the index to after the for loop so that it counts itself as one consecutive so "AA" would be 2 consecutive.

I need your definition of what consecutive should equal to make this calculate correctly though. Right now it's going to look at each character and add 1 to consecutive for each matching character after itself (includes itself).

    int totCharacters=0, vowels=0, digits=0, odds=0, consecutive=0, index=0;
    String text;
    char ch;

    Scanner input = new Scanner(System.in);
    System.out.print("Please enter a sentence terminated with a full stop or exclamation mark: ");
    text = input.nextLine();

    do {
        ch = text.charAt(index);

        if(ch=='.' && ch=='!')
            break;

        if(index<text.length()-1)
            totCharacters++;
        if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
            vowels++;
        if(ch>='0' && ch<='9')
            digits++;
        if(ch=='1' || ch=='3' || ch=='5' || ch=='7' || ch=='9')
            odds++;


    for(int i=index; i<text.length(); i++)
        {

             if(ch==text.charAt(i))
             {
                consecutive++;
             }
             else 
             {
                 break;
             }

        }
        index++;
        }while(ch!= '.' && ch!='!');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM