Reputation: 53
I am trying to create a program that loops through a string, and if it is a vowel that adds it to a variable and then displays. The idea is not to use regular expressions so I use a for. And the problem is that it does not show me the result well, can you help me?
import java.util.Scanner;
import java.lang.StringBuilder;
public class ReemplazarVocales {
public static void main(String[] args) {
Scanner InputText = new Scanner(System.in);
StringBuilder str = new StringBuilder();
System.out.println("Escribe una frase\n");
String Sentence = InputText.next();
Sentence = Sentence.toLowerCase();
char Vocal;
for (int i=0;i <= Sentence.length();i++){
Vocal = Sentence.charAt(i);
String Consonant = Character.toString(Vocal);
if (Consonant != "a" ||Consonant !="e" || Consonant !="i" || Consonant !="o" || Consonant!="u"){
str.append(Consonant);
}
}
System.out.println("\nTu frase sin vocales " + str);
}
}
Upvotes: 1
Views: 508
Reputation: 79085
I am trying to create a program that loops through a string, and if it is a vowel that adds it to a variable and then displays.
Sentence.length()
which is not possible because java stores the characters in a string starting with index, 0
and therefore the last index is equal to the length-of-the-string minus one. Trying to access an index beyond the limits results in StringIndexOutOfBoundsException
.char
into a String
in order to add it to a StringBuilder
; you can append a char
value directly to the StringBuilder
object.==
instead of !=
.Scanner#nextLine
instead of Scanner#next
which stops scanning the input as soon as it encounters whitespace.A. Replace
String Sentence = InputText.next();
with
String Sentence = InputText.nextLine();
B. Replace
for (int i=0;i <= Sentence.length();i++)
with
for (int i = 0; i < Sentence.length(); i++)
C. Replace
Vocal = Sentence.charAt(i);
String Consonant = Character.toString(Vocal);
if (Consonant != "a" ||Consonant !="e" || Consonant !="i" || Consonant !="o" || Consonant!="u"){
str.append(Consonant);
}
with
Vocal = Sentence.charAt(i);
// String Consonant = Character.toString(Vocal);// It's not needed
if (Vocal == 'a' || Vocal == 'e' || Vocal == 'i' || Vocal == 'o' || Vocal == 'u') {
str.append(Vocal);
}
I also recommend you follow the Java naming conventions e.g. Vocal
should be vocal
and Sentence
should be sentence
as per the naming conventions.
Upvotes: 1
Reputation: 132
There seem to be three problem in you code:
Since string indexing start from 0 you can loop like
for (int i=0;i < Sentence.length();i++){
Vocal = Sentence.charAt(i);
String VocalP = Character.toString(Vocal);
if (!VocalP.equals("a") && !VocalP.equals("e") && !VocalP.equals("i") && !VocalP.equals("o") && !VocalP.equals("u")){
str.append(VocalP);
}
}
you need to have (&&) instead of Logical (||) because you wish to eliminate all the vowels.
It is adviceable to do string comparision using equals or equalsIgnoreCase to compare two strings
Upvotes: 2
Reputation: 11
You wrote:
if (VocalP!="a" || VocalP!="e" || VocalP!="i" || VocalP!="o" || VocalP!="u")
which if you look closely will always be true. for example if you take the first two options only:
if (VocalP!="a" || VocalP!="e")
if the letter is not 'a' OR the letter is not 'b'
and the letter is 'a' it will still evaluate to true because it is not 'e' from your question you should be using == instead of != so try:
if (VocalP=="a" || VocalP=="e" || VocalP=="i" || VocalP=="o" || VocalP=="u")
Upvotes: -1