patternjavaModerate
Loop round and remove the consonants
Viewed 0 times
theloopremoveconsonantsroundand
Problem
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.
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 < test.length(); ktr++){
// System.out.println(test.substring(ktr, ktr+1));
check = test.substring(ktr, ktr+1);
if(!"b".equals(check) && !"c".equals(check)){
if(!"d".equals(check) && !"f".equals(check)){
if(!"g".equals(check) && !"h".equals(check)){
if(!"j".equals(check) && !"k".equals(check)){
if(!"l".equals(check) && !"m".equals(check)){
if(!"n".equals(check) && !"p".equals(check)){
if(!"q".equals(check) && !"r".equals(check)){
if(!"s".equals(check) && !"t".equals(check)){
if(!"v".equals(check) && !"w".equals(check)){
if(!"x".equals(check) && !"y".equals(check)){
if(!"z".equals(check)){
System.out.print(check);
}
}
}
}
}
}
}
}
}
}
}
}Solution
So it looks like you're just printing out the vowels in your string, right?
Removes the
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:
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 < 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 < names.length; i++)
{
System.out.println(names[i]);
}
}
}Code Snippets
System.out.println(answer.replaceAll("[^AaeEiIoOuU]", ""));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 < 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 < names.length; i++)
{
System.out.println(names[i]);
}
}
}Context
StackExchange Code Review Q#112242, answer score: 19
Revisions (0)
No revisions yet.