1

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);
    
    }
}

3 Answers3

2

There seem to be three problem in you code:

  1. you are looping from index int i=0 till i=length, which will give you IndexOutOfBound.

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);
            }
        }
  1. you need to have (&&) instead of Logical (||) because you wish to eliminate all the vowels.

  2. It is adviceable to do string comparision using equals or equalsIgnoreCase to compare two strings

Dharman
  • 30,962
  • 25
  • 85
  • 135
1

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.

  1. You are trying to get a character at the index = 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.
  2. You do not need to convert a char into a String in order to add it to a StringBuilder; you can append a char value directly to the StringBuilder object.
  3. You need to use == instead of !=.
  4. You need to use 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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Hi, I have two problems. When it encounters a space, the for loop breaks. If I use the operator (!=) I have to necessarily open an else, otherwise it adds all the letters. – Mariasmus Oreades Oct 22 '20 at 18:17
  • @MariasmusOreades - I've added point#4 in my answer to deal with this problem. Feel free to comment in case of any further issue/doubt. – Arvind Kumar Avinash Oct 22 '20 at 18:39
-1

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")
Harry
  • 11
  • 1
  • 4