Java isVowel() 方法在字符串中包含一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23191155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:15:46  来源:igfitidea点击:

isVowel() method with a character in a String

javachar

提问by user3505003

I have a text file with a bunch of words. One part of my program is supposed to, based on the user input, scan this list and insert into the output file the words that contain the requested number of vowels. I have a isVowelmethod that returns a boolean, but it doesn't seem to work--the output is just an empty list.

我有一个包含一堆单词的文本文件。我的程序的一部分应该根据用户输入扫描此列表并将包含所需元音数量的单词插入到输出文件中。我有一个isVowel返回 a的方法boolean,但它似乎不起作用——输出只是一个空列表。

Here's the method:

这是方法:

public boolean isVowel(char c)
{
    if(c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' ||     c=='u' || c=='U')
    {    
        return true;
    }    
    else
    {
        return false;
    }    
}
}   

Here's the part of the program where it is used:

这是使用它的程序部分:

public ArrayList<String> vowelHeavy(int n, int m)
{
    int vowels = 0;
    while(input.hasNextLine())
    {    
        word = input.nextLine();
        if (word.length() == n)
        {
            for(int i = 0; i < word.length(); i++)
            {
                if(isVowel(word.charAt(i)) == true)
                {
                    vowels++;
                }
            }
            if (vowels == m)
            {
                output.add(word);
            }
        }
        word = input.nextLine();
    }
    return output;
}

采纳答案by Bohemian

Your biggest problem is you are not resetting you vowelscounting variable when you test a new word. The value just keeps accumulating the total vowels in the file.

你最大的问题是vowels当你测试一个新词时你没有重置你的计数变量。该值只是不断累积文件中的总元音。

Change:

改变:

int vowels = 0;
while(input.hasNextLine()) {    
   ...

to:

到:

while(input.hasNextLine()) {    
   int vowels = 0;
   ...


As an aside, most of your code, including the isVowel()method, could be eliminated with a single line:

顺便说isVowel()一句,你的大部分代码,包括方法,都可以用一行来消除:

int vowels = word.replaceAll("[^aeiouAEIOU]", "").length();

This works by eliminating from the word all characters that aren't vowels; what's left are the vowels, so just take the length to get the count.

这是通过从单词中消除所有不是元音的字符来实现的;剩下的是元音,所以只需取长度来计算。

回答by cybersam

2 things:

2件事:

  1. If the word.length() == ntest is never true, then outputis never changed. Please make sure that test is correct.
  2. This statement exists at the top AND bottom of the while loop, causing you to ignore every other input line:

    word = input.nextLine();

  1. 如果word.length() == n测试永远不会为真,则output永远不会改变。请确保测试正确。
  2. 此语句位于 while 循环的顶部和底部,导致您忽略所有其他输入行:

    word = input.nextLine();

回答by Stephen Cave

Theres a much easier way to write this code:

编写此代码有一种更简单的方法:

public boolean isVowel(char c){
   return(your boolean tests);

}

}