-6

I don't understand why this method isn't working. It compiles, but throws a runtime error.

Here's the code. It's a Pig Latin-izer. It should split the phrase into words, then format those words, and then put them all back together in an ArrayList (I may change this later.) I don;t understand why this is not running correctly.

import java.util.*;
public class OinkerSpine 
{
    public ArrayList<String> pigLatin (String phrase)
    {
        phrase = phrase.replaceAll(".", " .");
        phrase = phrase.replaceAll(",", " ,");
        phrase = phrase.replaceAll("!", " !");
        //phrase = phrase.replaceAll("?", " ?");
        phrase = phrase.replaceAll("'", " '");

        String []words = phrase.split(" ");
        ArrayList <String> Endphrase = new ArrayList <String> ();
        final String AY = "ay";
        final String YAY = "-yay";

        String endword = "";

        for(int i=0; i < words.length; i++) 
        {
            String firstletter;
            String restofword;
            String secondletter;

            if (words[i].length() == 1)
            {
                firstletter = words[i];
                restofword = "";
            }
            else
            {
                firstletter = words[i].substring(0, 1);
                restofword = words[i].substring(1);
            }

            boolean firstIsUpper = (firstletter.equals(firstletter.toUpperCase()));

            if (firstIsUpper)
            {
                firstletter = firstletter.toLowerCase();
                secondletter = restofword.substring(0, 1);
                restofword = restofword.substring(1);

                secondletter = secondletter.toUpperCase();
                restofword = secondletter + restofword;
            }

            if (firstletter.equals("a") || firstletter.equals("e") ||
                firstletter.equals("i") || firstletter.equals("o") ||
                firstletter.equals("u"))
            {
                endword = firstletter + restofword + YAY;
            }

            else
            {
                endword = restofword + "-" + firstletter + AY;
            }

            endword = endword.replaceAll(" .", ".");
            endword = endword.replaceAll(" ,", ",");
            endword = endword.replaceAll(" !", "!");
            //endword = endword.replaceAll(" ?", "?");
            endword = endword.replaceAll(" '", "'");

            Endphrase.add(endword);
        }
        return Endphrase;
    }
}

What's up here?

DigiDuncan
  • 131
  • 1
  • 2
  • 11

2 Answers2

1

What will happen here if restofword or word[i] is an empty string

secondletter = restofword.substring(0, 1);

or

firstletter = words[i].substring(0, 1);
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

You're probably replacing too many characters here:

phrase = phrase.replaceAll(".", " .");

leaving insufficient letters for substring. You probably wanted

phrase = phrase.replaceAll("\\.", " .");

or better

phrase = phrase.replace(".", " .");
Reimeus
  • 158,255
  • 15
  • 216
  • 276