Loop round and remove the consonants The main objective of this code is that you must accept an input from the user and loop and remove the consonants and vice versa and display the new word.

I know there is a easier way of doing it, but I have no idea how.

String test = "john doe", check;
    for(int ktr = 0; ktr &lt; test.length(); ktr++){
       // System.out.println(test.substring(ktr, ktr+1));
        check = test.substring(ktr, ktr+1);
        if(!"b".equals(check) &amp;&amp; !"c".equals(check)){
            if(!"d".equals(check) &amp;&amp; !"f".equals(check)){
                if(!"g".equals(check) &amp;&amp; !"h".equals(check)){
                    if(!"j".equals(check) &amp;&amp; !"k".equals(check)){
                        if(!"l".equals(check) &amp;&amp; !"m".equals(check)){
                            if(!"n".equals(check) &amp;&amp; !"p".equals(check)){
                                if(!"q".equals(check) &amp;&amp; !"r".equals(check)){
                                    if(!"s".equals(check) &amp;&amp; !"t".equals(check)){
                                        if(!"v".equals(check) &amp;&amp; !"w".equals(check)){
                                            if(!"x".equals(check) &amp;&amp; !"y".equals(check)){
                                                if(!"z".equals(check)){
                                                     System.out.print(check);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }   
        }
    }
So it looks like you're just printing out the vowels in your string, right?

System.out.println(answer.replaceAll("[^AaeEiIoOuU]", ""));


Removes the for loop and compacts your code a lot, making it a lot more readable IMO.  Also notice how easy it was for me to extend to capital letters.



It would be good to note that this is a replacement for your block of code as it is right now.  If you plan on printing the vowels in a variable amount of strings (perhaps input by the user), then you would want to make a method that would do this for you.  In this function you would pass in the precompiled regex and the string to print.

I wrote this up really quick in a text editor and didn't compile it, but here is a program that could handle multiple strings better:

class VowelPrinter
{
    static final Pattern p = Pattern.compile("[^AaeEiIoOuU]");

    public static String removePattern(Pattern p, String str)
    {
        return p.matcher(str).replaceAll("");
    }
    public static void main (String[] args)
    {
        int num = 0;

        System.out.print("How many names are you going to save: ");
        Scanner in = new Scanner(System.in);
        num = Integer.parseInt(in.nextLine().trim());

        String names[] = new String[num];
        for (int i = 0; i &lt; names.length; i++)
        {
            System.out.print("Type a name: ");
            names[i] = removePattern(p, in.nextLine());
        }
        System.out.println("Names with consonants removed:");
        for (int i = 0; i &lt; names.length; i++)
        {
            System.out.println(names[i]);
        }
    }
}