Cody G.
Cody G.

Reputation: 53

manipulating strings in java

Hi so I'm having trouble manipulating strings in java. My problem is to search through a string and when a vowel is found I want to insert another string at the position. Here is what I have:

Scanner scan = new Scanner(input.getText());

    while(scan.hasNext()){
        String str = scan.next();
        str = str.toUpperCase();
        String str1 = "";

        for (int i = 0; i < str.length(); i++){
            if (str.charAt(i) == 'A' || str.charAt(i) == 'E'
                    || str.charAt(i) =='I' || str.charAt(i) == 'O'
                    || str.charAt(i) == 'U'){

                    str1 = str.substring(0 , i) + "AHHH" + str.substring(i);

            }
        }

        System.out.print(str1);
    }

So if the string thats being read in by scanner is hello it should return:

HAHHHELLAHHHO

My program is returning:

HAHHHELLOHELLAHHHO

So my program is finding the first vowel adding AHHH and then concatenating it with the rest of the string. Then it finds the next vowel and does the same thing.

Anyone know how I could better manipulate this string or is this possible with just using a string?

Upvotes: 2

Views: 101

Answers (4)

Elliott Frisch
Elliott Frisch

Reputation: 201537

I would prefer a StringBuilder, further I would prefer String.toUpperCase() and String.toCharArray() with a for-each loop. For example,

String str = "hello";
StringBuilder sb = new StringBuilder();
for (char ch : str.toUpperCase().toCharArray()) {
    if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
        sb.append("AHHH");
    }
    sb.append(ch);
}
System.out.println(sb);

Outputs (as requested)

HAHHHELLAHHHO

Another option, use a regular expression to group vowels and replace them with "AHHH" and the group. Like,

System.out.println(str.toUpperCase().replaceAll("([AEIOU])", "AHHH$1"));

Upvotes: 2

Robby Cornelissen
Robby Cornelissen

Reputation: 97382

Your problem is here:

str1 = str.substring(0 , i) + "AHHH" + str.substring(i);
//                   ^

Every time you find a vowel, you append a substring beginning at the start of the original string again.

Secondly, concatenating strings in a loop is inefficient, and it's better to use a StringBuilder.


Putting everything together:

import java.util.*;

public class Test {
  public static void main(String[] args) {
    String str = "hello";
    StringBuilder sb = new StringBuilder();

    for (char c : str.toUpperCase().toCharArray()) {
      if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
        sb.append("AHHH");
      }

      sb.append(c);
    }   

    System.out.print(sb.toString());
  }
}

Alternatively, you could just use replaceAll() with a regular expression:

public class Test {
  public static void main(String[] args) {
    String str = "hello";
    String result = str.toUpperCase().replaceAll("([AEIOU])", "AHHH$1");

    System.out.println(result);
  }
}

Upvotes: 1

Zmalski
Zmalski

Reputation: 35

Everytime your loop finds a vowel, it concatenates HELLO again. So when it hits E str1 looks like "HAHHHELLO", but once it hits the O, it's adding HELLO again, because you're using the substrings of the original string.

Upvotes: 0

Kartik
Kartik

Reputation: 7917

Change your for-loop to this:-

for (int i = 0; i < str.length(); i++) {
    if (str.charAt(i) == 'A' || str.charAt(i) == 'E'
            || str.charAt(i) == 'I' || str.charAt(i) == 'O'
            || str.charAt(i) == 'U') {
        str1 += "AHHH";
    }
    str1 += str.charAt(i);
}

Here is the full working example:-

public static void main(String[] args) {
    String str = "hello".toUpperCase();
    String str1 = "";

    for (int i = 0; i < str.length(); i++) {
        if (str.charAt(i) == 'A' || str.charAt(i) == 'E'
                || str.charAt(i) == 'I' || str.charAt(i) == 'O'
                || str.charAt(i) == 'U') {
            str1 += "AHHH";
        }
        str1 += str.charAt(i);
    }
    System.out.print(str1);
}

Better to use StringBuilder in loops though.

Upvotes: 0

Related Questions