Mariasmus Oreades
Mariasmus Oreades

Reputation: 53

How to go through a string and if it is a vowel add it with a for (String index out of range)

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

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

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.

  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.

Upvotes: 1

Shivani Shanishchara
Shivani Shanishchara

Reputation: 132

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

Upvotes: 2

Harry
Harry

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

Related Questions