简体   繁体   English

检查一个字符连续出现在字符串中的次数

[英]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. 我假设问题在于它没有按您期望的方式consecutive计数。 The problem is that the for loop is running after you've gone through all the characters. 问题在于,遍历所有字符后, for循环正在运行。 Since ch is set the last time when a '.' 由于ch是最后一次设置为'。'时设置的。 or '!' 要么 '!' is encountered, the for loop is just counting all the '.' 遇到时,for循环只是计数所有的'。 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. 另外,如果要计数连续数,则可以保留currentChar变量,并检查下一个变量是否等于它。 If it does, consecutives++ 如果有,则consecutives++

This will count ALL consecutive chars ( i believe, can't compile atm). 这将计算所有连续的字符(我相信不能编译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. 我将for循环移至do while内,以便您对每个字符进行处理。 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. 我还将基于索引的int i变量作为基础,以便您不会在正在检查的当前字符之前查看字符。 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. 也将索引的增量移动到for循环之后,以使其自身算作连续一个,因此“ AA”将连续2个。

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). 现在,它将查看每个字符,并为每个匹配的字符在其自身(包括自身)之后添加1。

    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!='!');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取字符在字符串中出现多少次时出错 - errors reading how many times a character occurs in a string 检查字符串在符合条件的字符串中包含字符“ g”的次数 - check how many times string contains character 'g' in eligible string 如何在不使用s循环的情况下计算字符多次出现在字符串中 - how to count many times a character occurs in a string without using s loop 特定字符在字符串中出现的次数 - How many times a specific character is in a String 第一个字符出现在字符串中的次数 - How many times the first character appears in a string 一个数字出现多少次 - How many times a number occurs 应返回字符串中连续重复 3 次的字母数,不使用正则表达式...仅使用核心概念 - Should return how many letters repeated consecutively 3 times in a string,without using regex...use only core concepts 您如何在数组/字符串中找到特定值以及在数组/字符串中出现了多少次 - How do you find a specific value in an array/string and how many times it occurs in the array/string 如何从字符串中获取重复字符以及重复多少次 - how to get Repeated character from String and how many times it repeated 检查输入条目中的每个位置并返回出现字符的次数 - Check each position in the input entry and return the number of times a character occurs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM