首页 > 解决方案 > 计算字符串上的匹配字符

问题描述

我被要求创建一个程序,要求用户输入两个输入,这两个输入都必须存储为字符串。第一个输入可以是一个或多个单词,第二个输入必须是一个单独的字符。在用户输入两个输入后,程序应该计算唯一的宪章出现在第一个字符串中的次数(如果有的话)。一旦完成第一个字符串的迭代,程序就应该输出第二个字符串的实例数。例子:

“测试中有 1 次出现‘e’。”

程序必须使用 while 循环和字符串值。这是我现在遵循教授建立的参数的解决方案

public static void main(String[] args) {
        String inputEntry; // User's word(s)
        String inputCharacter; // User's sole character
        String charCapture; // Used to create subtrings of char
        int i = 0; // Counter for while loop
        int charCount = 0; // Counter for veryfiying how many times char is in string
        int charCountDisplay = 0; // Displays instances of char in string
        Scanner scan = new Scanner(System.in);

        System.out.print("Enter some words here: "); // Captures word(s)
        inputEntry = scan.nextLine(); 

        System.out.print("Enter a character here: "); // Captures char
        inputCharacter = scan.nextLine();

        if (inputCharacter.length() > 1 ||  inputCharacter.length() < 1) // if user is not in compliance
        {
            System.out.print("Please enter one character. Try again.");
            return; 
        } 
         else if (inputCharacter.length() == 1) // if user is in compliance
         {
          while( i < inputEntry.length()) // iterates through word(s)
           {
              charCapture = inputEntry.substring(charCount); // Creates substring of each letter in order to compare to char entry
                    if (charCapture.equals(inputCharacter))
                    {
                        ++charCountDisplay;

                    }

                    ++charCount;  
                    ++i;
            }

          System.out.print("There is " + charCountDisplay + 
                  " occurrence(s) of " + inputCharacter + " in the test.");

         }



        }

这个迭代有一个错误。它不会计算 inputCharacter 变量的所有实例,而不管字符串上出现了多少实例,它只计数到一个。我知道问题出在这部分代码中:

while( i < inputEntry.length()) // iterates through word(s)
           {
              charCapture = inputEntry.substring(charCount); // Creates substring of each letter in order to compare to char entry
                    if (charCapture.equals(inputCharacter))
                    {
                        ++charCountDisplay;

                    }

                    ++charCount;  
                    ++i;
            }

我只是无法安静地确定我做错了什么。在我看来,charCountDisplay 变量在每次迭代后都会恢复为零。不应该在一开始就声明变量来避免这种情况吗?...我是一个困惑的人。

标签: javastringwhile-loopchar

解决方案


这是错误的

charCapture = inputEntry.substring(charCount);

不返回一个字符

尝试使用inputEntry.charAt(charCount)

另一个提示是将变量定义在靠近使用它们的位置而不是在方法的顶部,例如:

String inputEntry; 
inputEntry = scan.nextLine(); 

更好的是内联

String inputEntry = scan.nextLine(); 

它将使您的代码更加简洁和可读。

执行代码的更简洁的方法是:

Scanner scan = new Scanner(System.in);

System.out.print("Enter some words here: "); // Captures word(s)
String inputEntry = scan.nextLine(); 

System.out.print("Enter a character here: "); // Captures char
String inputCharacter = scan.nextLine();

// validate

// then

int len = inputEntry.length();
inputEntry = inputEntry.replace(inputCharacter, "");
int newlen = inputEntry.length();
System.out.format("There is %d occurrence(s) of %s in the test.%n", 
                                            len - newlen, inputCharacter);

输出

Enter some words here: scarywombat writes code
Enter a character here: o
There is 2 occurrence(s) of o in the test.

推荐阅读