0

This lab for my intro class requires us to return any user input in pig latin. The error I seem to keep getting when I run it and type any string into the console is this:

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

The error is found here:

 thisWord = x.substring(indexOfCurrentNonLetter +1, indexOfNextNonLetter);

I understand from another post this is the problem:

IndexOutOfBoundsException -- if the beginIndex is negative, or endIndex is >larger than the length of this String object, or beginIndex is larger than >endIndex.

Actually, your right edge of your substring function may be lower than the left >one. For example, when i=(size-1) and j=size, you are going to compute >substring(size-1, 1). This is the cause of you error. (Answer By Bes0ul)

My question is, what is the solution to this problem?

Here are the methods I am using:

public String pigLatin(String x)
{
    String thisWord = "";
    x += " ";
    int indexOfNextNonLetter = 0, indexOfCurrentNonLetter = 0;
    for(int i = 0; i < x.length(); i++)
    {
        System.out.println("At least the loop works"); //Let's play a game called find the issue
        if(x.charAt(i) < 65 || x.charAt(i) > 90 && x.charAt(i) < 97 || x.charAt(i) > 122) //if it isn't a letter
        {
            phrase += x.charAt(i); // add our non character to the new string
            for(int j = 0; j < x.substring(i).length(); j++) // find the next character that isn't a letter
            {
                if(x.charAt(i) < 65 || x.charAt(i) > 90 && x.charAt(i) < 97 || x.charAt(i) > 122)
                {
                    indexOfNextNonLetter = j + i;
                    if(j+i > x.length())
                    System.out.print("Problem Here");
                    indexOfCurrentNonLetter = i;
                    System.out.println("noncharacter detected"); //I hope you found the issue
                    j = x.length();
                    thisWord = x.substring(indexOfCurrentNonLetter +1, indexOfNextNonLetter);//between every pair of nonletters exists a word
                }
            }
            phrase += latinWord(thisWord); // translate the word
            i = indexOfNextNonLetter - 1;
        }
    }
    return phrase;
}

/*
 * This converts the passed word to pig latin
 */
public String latinWord(String word)
{
    String lWord = "";
    String beforeVowel = "";
    String afterVowel = "";
    boolean caps = false;
    char first = word.charAt(0);
    if(first > 'A' && first < 'Z')
    {
        first = Character.toLowerCase(first); //If the first char is capital, we need to account for this
        caps = true;
    }
    if(containsNoVowels(word))
        lWord = Character.toUpperCase(first) + word.substring(1) + "ay"; //If we have no vowels
    if(word.charAt(0) == 'A' || word.charAt(0) == 'a' || word.charAt(0) == 'E' || word.charAt(0) == 'e' || word.charAt(0) == 'I' || word.charAt(0) == '0'
    || word.charAt(0) == 'O' || word.charAt(0) == 'O' || word.charAt(0) == 'o' || word.charAt(0) == 'U' || word.charAt(0) == 'u')
    {
        lWord = Character.toUpperCase(first) + word.substring(1) + "yay"; //If the first char is a vowel
    }
    if(word.charAt(0) != 'A' || word.charAt(0) != 'a' || word.charAt(0) != 'E' || word.charAt(0) != 'e' || word.charAt(0) != 'I' || word.charAt(0) != '0'
    || word.charAt(0) != 'O' || word.charAt(0) != 'O' || word.charAt(0) != 'o' || word.charAt(0) != 'U' || word.charAt(0) != 'u')
    { //If the first letter isnt a vowel but we do have a vowel in the word
        for(int m = 0; m < word.length(); m++)//if we have a vowel in the word but it doesn't start with a vowel
        {
            if(word.charAt(m) == 'A' || word.charAt(m) == 'a' || word.charAt(m) == 'E' || word.charAt(m) == 'e' || word.charAt(m) == 'I' || word.charAt(m) == 'm'
            || word.charAt(m) == 'O' || word.charAt(m) == 'O' || word.charAt(m) == 'o' || word.charAt(m) == 'U' || word.charAt(m) == 'u')
            {                    
                for(int l = 0; l < word.substring(m).length(); l++)
                {
                    afterVowel += word.substring(m).charAt(l); //Build the part after the first vowel
                }
                m += word.length();
            }
            else
                beforeVowel += word.charAt(m);
        }
        if(caps == false)
            lWord = afterVowel + beforeVowel + "ay";
        else
        {
            first = Character.toUpperCase(afterVowel.charAt(0));
        }
    }

    return lWord;
}

/*
 * This function checks the string letter by letter to see if any of the letters are vowels.If there are none it returns true
 */
public boolean containsNoVowels(String wrd)
{
    for(int h = 0; h < wrd.length(); h++)
    {
        if(wrd.charAt(h) == 'A' || wrd.charAt(h) == 'a' || wrd.charAt(h) == 'E' || wrd.charAt(h) == 'e' || wrd.charAt(h) == 'I' || wrd.charAt(h) == 'h'
        || wrd.charAt(h) == 'O' || wrd.charAt(h) == 'O' || wrd.charAt(h) == 'o' || wrd.charAt(h) == 'U' || wrd.charAt(h) == 'u')
            return false;
        else
            return true;
    }
    return false;
}
1
  • 2
    Question tags corrected as your question has nothing to do with Apache-Pig nor Latin. Commented Oct 28, 2015 at 1:36

1 Answer 1

1

I think the error lies inside your second for loop within pigLatin():

for (int j = 0; j < x.substring(i).length(); j++) // find the next character that isn't a letter
{
     if(x.charAt(i) < 65 || x.charAt(i) > 90 && x.charAt(i) < 97 || x.charAt(i) > 122)

The last line shown above starts checking at char i. This is the same char you just checked in the outer loop. So the test will succeed. And I think the logic breaks down either in this iteration or a subsequent one.

I think what you want is:

for(int j = 1; j < x.substring(i).length(); j++)
{
     if (x.charAt(j) < 65 || x.charAt(j) > 90 && x.charAt(j) < 97 || x.charAt(j) > 122)

Note, there are two changes:

  1. Initialise j at 1 to test the next character in the for statement
  2. Test the jth character, not the ith in the if statement
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.