需要打印没有元音的扫描仪字符串的代码

时间:2013-12-09 15:09:07

标签: java string printing character

我需要编写一个带有字符串的程序,并打印出删除了所有元音的字符串文本,除非一个单词以它开头。

我写的代码就在那里,但我无法弄清楚为什么它不会返回整个字符串而且它不会删除所有的元音。假设我输入短语“Desirable property area”。程序打印字符串“Dirlp”而不是“Dsrbl prprty ar”

有人可以建议我如何改进代码以使其工作吗?谢谢!

这是我的代码:

public static void main (String [] args)
{
    Scanner in = new Scanner (System.in);
    System.out.print ("Enter some text, then hit enter: ");
    String text = in.nextLine();

    takeOutVowel (text);
    System.out.println ();
}

static void takeOutVowel (String s)
{
    char ch = s.charAt(0);                              //character to be printed
    System.out.print (ch);
    int nextCh = 1;                                 //determines the position of the next character
    int increase = 1;                               //increase is how much i will increase by in the for loop; takes the position of vowels into                                                        //consideration so they can be skipped

    for (int i = 1; i <= s.length(); i += increase)
    {
        ch = s.charAt (nextCh);

        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
        {
            nextCh++;
            ch = s.charAt (nextCh); 

            while (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
            {
                nextCh++;
                ch = s.charAt (nextCh);

                if (nextCh >= s.length())
                {
                    ch = ' ';
                    break;          
                }   

            } 
        }

        System.out.print (ch);  
        nextCh++;
        ch = s.charAt (nextCh);

        if (ch == ' ')                                  //if the previous position was a space, then this will allow for the vowel to be printed
        {
            System.out.print ("" + ch + s.charAt(nextCh + 1));
            nextCh++;
        }

        increase = nextCh;


    }

感谢目前为止的所有答案 - 非常有帮助!我不允许使用数组或任何尚未涵盖的内容,因此我将代码修改为以下内容。它编译得很好但是当我运行程序并输入扫描仪文本时,我收到一条消息说

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 27
at java.lang.String.charAt(String.java:686)
at Vowels.noVowels(Vowels.java:20)
at Vowels.main(Vowels.java:11)

我无法确定现在的问题。再次感谢您的帮助!

    import java.util.*;

class Vowels
{
    public static void main (String [] args)
    {
        Scanner in = new Scanner (System.in);
        System.out.print ("Enter some text, then hit enter: ");
        String text = in.nextLine();

        System.out.println (noVowels(text));
    }

    static String noVowels (String s)
    {
        String noVowels = "" + s.charAt(0);                 //Starts a new string with the first character of the input text

        for (int i = 1; i <= s.length(); i++)
        {
            if (isVowel(s.charAt(i)) && s.charAt(i-1) != ' ')       //if the character is a vowel and it's previous character wasn't a space, then this is a vowel to be                                                replaced
            {
                noVowels = noVowels.concat("");
            }

            else
            {
                noVowels = noVowels.concat("" + s.charAt(i));
            }
        }

        return noVowels; 
    }

    static boolean isVowel (char ch)
    {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
        {
            return true;
        }

        else
        {
            return false;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您可以轻松解决此问题。迭代字符串并检查元音。如果没有元音而不是附加结果。尝试

static void takeOutVowel(String s) {
    StringBuilder res = new StringBuilder();
    String[] words = s.split(" +");
    for (String word : words) {
        res.append(word.charAt(0)); //Skip the first char  
        for (int i = 1; i < word.length(); i++) {
            char ch = word.charAt(i);
            if (!isVowel(ch)) { // Check the vowel
                res.append(ch);
            }
        }
        res.append(' ');
    }

    System.out.println(res);
} 

static boolean isVowel(char ch){
  ch=Character.toLowerCase(ch); // Make it case-insensitive.
 return ch=='a' ||ch=='e' ||ch=='i' ||ch=='o' ||ch=='u';
}

答案 1 :(得分:1)

您也可以使用正则表达式轻松完成此操作。第一个例子演示了如何简单地删除元音。

public static void main(String[] args)
{
    String noVowels = takeOutVowel("Hello how are you?");
    System.out.println(noVowels); // prints "Hll hw r y?"
}

// This will remove all vowels from any String
private static String takeOutVowel(String s)
{
    return s.replaceAll("[aeiou]", ""); 
}

但是现在为了满足你忽略一个单词的第一个字母的要求,如果它是一个元音(这意味着我们将忽略它,无论如何),你只需要稍微编辑takeOutVowel方法。 / p>

static String takeOutVowel (String s)
{
   // split your string so we can examine each word separately
   String[] words = s.split(" ");
   String noVowels = "";

   for (int i = 0; i < words.length; i++){
       char firstChar = words[i].charAt(0);
       String temp = words[i].substring(1, words[i].length()).replaceAll("[aeiou]", "");
       noVowels += firstChar + temp + " ";
   }

   return noVowels;
} 

答案 2 :(得分:0)

这适用于您的以下要求 - 删除除单词以元音开头的所有元音。

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter some text, then hit enter: ");
    String text = in.nextLine();
    takeOutVowel(text);
    System.out.println();
}

static void takeOutVowel(String s) {
    String[] array = s.split(" ");                          // Split sentence into words and store them in an array.
    StringBuilder finalString = new StringBuilder();
    for (String word : array) {                             //For each word in the array
        char ch = word.toLowerCase().charAt(0);             //check if the lower case first character is a vowel.
        if(ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u'){    // When it is not, replace all the vowels.
            finalString = finalString.append(word.replaceAll("[aeiou]", "")).append(" ");  
        }else{
            finalString = finalString.append(ch).append(word.replaceAll("[aeiou]", "")).append(" ");// When it is , replace all the vowels except the first character.
        }
    }
    System.out.println(finalString);
}
}