-3

I am trying to write a compression program that removes vowels, using a String instead of an array, per the assignment specifications, but I keep getting this error. I am totally lost. Could use some help.

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

static String stringCompress(String msg) {

    String vowels = VOWELS;
    String output = "";
    String localChar;

   for (int i = 0; i < msg.length(); i++) {
       localChar = String.valueOf(msg.charAt(i));
       if (!VOWELS.contains(localChar)|| Character.isWhitespace(msg.charAt(i-1))){
           output += localChar;

       }
   }
    return output;
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

0
Solved

String output = "";
        String localChar;
        if(msg.isEmpty()) {
            output = "";
            return output;
        }
        output = String.valueOf(msg.charAt(0));
       for (int i = 1; i <= msg.length()-1; i++) {
           localChar = String.valueOf(msg.charAt(i));
           if (!VOWELS.contains(localChar)|| Character.isWhitespace(msg.charAt(i-1))){
               output += localChar;

           }
       }