3

StringBuilder to hide vowels:

String bienvenue_intro = " Welcome! Java First Semester: 455, java street: City (State): Country: 575757 ";
StringBuilder sb = new StringBuilder(bienvenue_intro);
String[] introduction = bienvenue_intro.split(":"); 
for (int i = 0; i < bienvenue_intro.length(); i++) {
    char c = bienvenue_intro.charAt(i);
    if ((c == 'A') || (c == 'a') ||
        (c == 'E') || (c == 'e') ||
        (c == 'I') || (c == 'i') ||
        (c == 'O') || (c == 'o') ||
        (c == 'U') || (c == 'u')) {
            sb.setCharAt(i, '*');
    }
}
System.out.println(bienvenue_intro);
System.out.println(sb.toString());

The output of the above code is:

Welcome! Java First Semester: 455, java street: City (State): Country: 575757 
 W*lc*m*! J*v* F*rst S*m*st*r: 455, j*v* str**t: C*ty (St*t*): C**ntry: 575757 

Method split to break the lines:

for (int i = 0; i < introduction.length; i++)
    System.out.println(introduction[i]);

The desired output using split + string builder would be:

W*lc*m*! J*v* F*rst S*m*st*r

 455, j*v* str**t

 C*ty (St*t*)

 C**ntry

 575757 

But both together it does not work! Is it even possible to team StringBuilder with the Split method?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Jane Nice
  • 97
  • 1
  • 1
  • 8

4 Answers4

6

Don't split the String before, first to the transformation. StringBuilder will perform the in-place character replacement for you and get the replaced String using toString() and perform the split on it.

String bienvenue_intro = " Welcome! Java First Semester: 455, java street: City (State): Country: 575757 ";
StringBuilder sb = new StringBuilder(bienvenue_intro);

for (int i = 0; i < bienvenue_intro.length(); i++) {
  char c = bienvenue_intro.charAt(i);
  if (   (c == 'A') || (c == 'a')
      || (c == 'E') || (c == 'e')
      || (c == 'I') || (c == 'i')
      || (c == 'O') || (c == 'o')
      || (c == 'U') || (c == 'u')) {
    sb.setCharAt(i, '*');
  }
}
System.out.println(bienvenue_intro);
//System.out.println(sb.toString());

String[] introduction = sb.toString().split(":");  //<-- Do the split here after replacements.
for (String string : introduction) {
  System.out.println(string);
}

Output:

 W*lc*m*! J*v* F*rst S*m*st*r
 455, j*v* str**t
 C*ty (St*t*)
 C**ntry
 575757 
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
3

There is a better and simpler way of doing this :)

replaceAll() and split().

public static void main(String[] args) {
    String bienvenue_intro = " Welcome! Java First Semester: 455, java street: City (State): Country: 575757 ";
    String[] arr = bienvenue_intro.replaceAll("(?i)[aeiou]+", "*").split(":");

    for(String s : arr) {
        System.out.println(s);
    }
}

 W*lc*m*! J*v* F*rst S*m*st*r
 455, j*v* str*t
 C*ty (St*t*)
 C*ntry
 575757 
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

I would like to expand on the answer given by @YoungHobbit. I am not an expert but these are my observations:

My take:

String bienvenue_intro = " Welcome! Java First Semester: 455, java street: City (State): Country: 575757 ";
String mod_str = bienvenue_intro.replaceAll( "(?i)[aeiou]", "*" );
String[] introduction = mod_str.split(":");
for (String string : introduction) {
  System.out.println(string);
}
Community
  • 1
  • 1
Suhas K
  • 380
  • 3
  • 14
0

I believe your approach is all wrong: There is already an API to do this in one, easily-readable, line of code:

System.out.println(bienvenue_intro.replaceAll("[aeiouAEIOU]", "*").replace(":", "\n"));
Bohemian
  • 412,405
  • 93
  • 575
  • 722