0

So I have this and be aware that I only use simple methods such at toString(), charAt. Right now my code is just returning the original string, and I do not know why.

Ok, so I realized that after a few tests, the reason it is returning the original string is because in the nested if statement in the loop, the condition is never true, so it bypasses the if statement. Why is it never true?

        System.out.print("Enter in a string: ");

        String userInput = scan.nextLine();//string user enters

        String vowelChar;//individual character within user's input

        for (int i=0; i<userInput.length(); i++){

            Character indChar = userInput.charAt(i);

            vowelChar = indChar.toString();

            if (vowelChar=="a" || vowelChar=="e" || vowelChar=="i" || vowelChar=="o" || vowelChar=="u"){
                String beforeText = userInput.substring(0, i-1);//string before vowel
                String afterText = userInput.substring(i+1);//string after vowel
                userInput=beforeText+"_"+afterText;
            }else{
                //character is not a vowel
                //do nothing
            }
        }
        System.out.print(userInput);
    }
}
James Zaghini
  • 3,895
  • 4
  • 45
  • 61
Joe Bob
  • 1
  • 2

5 Answers5

2

A few things to consider:

  1. Don't use "==" when comparing Strings, as explained in: Why doesn’t == work on String? You can compare chars with "==", so you shouldn't need to convert it to a String for the comparison.

  2. The index in your for loop starts at 0, so this statement:

    String beforeText = userInput.substring(0, i-1)

    will throw a java.lang.StringIndexOutOfBoundsException if there is a vowel at the first index.

  3. You don't need the "else" case if you aren't doing anything inside it.

Although this isn't how I would implement the kind of loop you wanted, here is a solution that works with the least amount of changes to your original code:

        System.out.print("Enter in a string: ");

        String userInput = scan.nextLine();//string user enters

        for (int i = 0; i < userInput.length(); i++) {

            Character indChar = userInput.charAt(i);

            if (indChar == 'a' || indChar == 'e' || indChar == 'i' || indChar == 'o' || indChar == 'u' ||
                    indChar == 'A' || indChar == 'E' || indChar == 'I' || indChar == 'O' || indChar == 'U') {
                String beforeText = userInput.substring(0, i); //string before vowel
                String afterText = userInput.substring(i + 1);  //string after vowel
                userInput = beforeText + "_" + afterText;
            }
        }
        System.out.print(userInput);
Community
  • 1
  • 1
ejtt
  • 363
  • 3
  • 10
1

Instead of converting back to String and comparing with == (since we compare the value of Strings via Object#equals), use the char type for your comparison using if/switch. Additionally you should compare in a singular casing so as not to have A and a not match.

char c = Character.toLowerCase(userInput.charAt(i)); //make the character lowercase
switch (c) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        //replace vowel
        break;
}
Rogue
  • 11,105
  • 5
  • 45
  • 71
0

There is no need to convert to a String. Compare characters directly

Character indChar = userInput.charAt(i);
if ((vowelChar == 'a' || vowelChar == 'e' || vowelChar == 'i' || vowelChar=='o' || vowelChar=='u') {
    // ...
}

Note the char in single quotes and not double quotes

aldrin
  • 4,482
  • 1
  • 33
  • 50
0

Change your code like this

if (vowelChar.equals("a") || vowelChar.equals("e") || vowelChar.equals("i") || vowelChar.equals("o") || vowelChar.equals("u")){

Or

if (indChar =='a' || indChar =='e' || indChar =='i' || indChar =='o' || indChar =='u'){
Sunil Singh Bora
  • 771
  • 9
  • 24
-1
    String a = "abcde";
    Character indChar = a.charAt(0);

    String vowelChar = indChar.toString();

    System.out.println("vowelChar Char: " + vowelChar);

    System.out.println(vowelChar == "a");  printed false
    System.out.println(vowelChar.equals("a")); printed true

So use equals instead of ==

Lyju I Edwinson
  • 1,764
  • 20
  • 20