Megan Majewski Megan Majewski - 4 months ago 26x
Java Question

Java Letter changes arraylist to string without commas but including white spaces

Trying to complete this challenge from coderbyte: "Using the Java language, have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string."

The problem that i am having is the replace is pulling on the white spaces between characters, but I need it to preserve white spaces between words. Is there a better solution to this?

import java.util.Arrays;
import java.util.Scanner;

public class nextLetter {
public static String LetterChanges(String str) {
String[] inputString = str.replaceAll("[^a-zA-Z ]", "").split("");
String[] alph= "abcdefghijklmnopqrstuvwxyz".split("");
String[] vowel ="aeiouy".split("");
for(int i=0; i<inputString.length; i++){
int index= Arrays.asList(alph).indexOf(inputString[i])+1;
inputString[i]= alph[index];
inputString[i]= inputString[i].toUpperCase();
return Arrays.toString(inputString)
.replace(" ","")
.replace(",", "") //remove the commas
.replace("[", "") //remove the right bracket
.replace("]", "")//remove the left bracket
.replace(" ","")
public static void main(String[] args) {
Scanner s = new Scanner(;
System.out.println("enter a sentence");

Also I would not mind any pointers on how to improve this!


This preserves all other characters and handles the vowels.

public static String LetterChanges(String str)
    str = str.toLowerCase();
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < str.length(); i++)
        char c = str.charAt(i);

        if ('a' <= c && c <= 'z')
            c = (c == 'z') ? 'a' : (char) (c + 1);

            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
                c = Character.toUpperCase(c);
    return sb.toString();

Input: abcdefghijklmnopqrstuvwxyz 1234567890

Output: bcdEfghIjklmnOpqrstUvwxyzA 1234567890