0

Whenever I try executing this code, it returns an error whenever I enter a one letter word though the compiling is fine and doing a phrase with any other word more than one letter\

Im confused why this is, because everything should return as fine, but whenever the code goes through a one letter word, it automatically stops the program and ends it

import javax.swing.*;
// The "PigTrans" class.
public class PigTrans
{
public static void main (String[] args)
{

    String userInput;
    userInput = JOptionPane.showInputDialog (null, "Please enter a word");

    System.out.println (PigTrans.pigTranslator (userInput));

} // main method


public static String pigTranslator (String word)
{


    int[] anArray;
    String phrase = "";
    String[] words = word.split ("\\s+");

    String[] newWord = new String [words.length];


    for (int i = 0 ; i < words.length ; i++)


        {
            char number;
            number = words [i].charAt (0);
            int ascii;

            ascii = (int) number;
            if (word.length () <= 1)
            {
                if ((ascii >= 48) && (ascii <= 57))
                {
                    newWord [i] = words [i];
                    phrase = phrase + newWord [i] + " ";
                }
                else
                {
                    newWord [i] = words [i];
                    phrase = phrase + newWord [i] + " ";
                }
            }


            else if ((ascii >= 65) && (ascii <= 90)) //Checks if the word begins with a capital
            {

                char word2;
                word2 = words [i].charAt (0);
                if (startsWithQU (words [i]) == true)
                {
                    newWord [i] = words [i].substring (2);
                    newWord [i] = newWord [i] + "quay";
                }

                else if (checkAscii (word2) == true)
                {
                    newWord [i] = words [i] + "way";
                }
                else
                {
                    String letter = "";
                    letter = words [i].substring (0, 1);
                    newWord [i] = words [i].substring (1);
                    newWord [i] = newWord [i] + letter + "ay";
                }
                String firstLetter, restOfWord, FirstLetter2;
                firstLetter = newWord [i].substring (0, 1);
                restOfWord = newWord [i].substring (1);
                restOfWord = restOfWord.toLowerCase ();
                firstLetter = firstLetter.toUpperCase ();
                newWord [i] = firstLetter.concat (restOfWord);


                phrase = phrase + newWord [i] + " ";
            }
            else
            {
                char word2;
                word2 = words [i].charAt (0);
                if (startsWithQU (words [i]) == true)
                {
                    newWord [i] = words [i].substring (2);
                    newWord [i] = newWord [i] + "quay";
                }

                else if (checkAscii (word2) == true)
                {
                    newWord [i] = words [i] + "way";
                }
                else
                {
                    String letter = "";
                    letter = words [i].substring (0, 1);
                    newWord [i] = words [i].substring (1);
                    newWord [i] = newWord [i] + letter + "ay";
                }
                phrase = phrase + newWord [i] + " ";
            }

        }



    return phrase;
} //pigTranslator


public static boolean checkAscii (char letter)
{
    boolean correct = false; //Declares the boolean variable as false

    int ascii;

    ascii = (int) letter;

    if ((ascii == 65) || (ascii == 69) || (ascii == 73) || (ascii == 79) || (ascii == 85) || (ascii == 97) || (ascii == 101) || (ascii == 105) || (ascii == 111) || (ascii == 117))
    {
        correct = true;
    }

    return correct;
} // PigMethods class

public static boolean startsWithQU (String word)
{
    boolean checkQU = false; //Declares variables needed to check for "qu." Program takes two letters from the word and check if it starts with "qu."
    String QUcheck;

    QUcheck = word.substring (0, 2);

    if (QUcheck.equalsIgnoreCase ("qu")) //When the word starts with "qu, return of boolean value becomes true."
    {
        checkQU = true;
    }

    return checkQU; //If return value is not true, the varuiable returns the value of checkQU.

} //startsWithQU
 } // PigTrans class
  • What error message are you seeing? What line throws the exception? – Hovercraft Full Of Eels May 12 '14 at 02:59
  • java.lang.StringIndexOutOfBoundsException: String index out of range: 2 at java.lang.String.substring(Unknown Source) at PigTrans.startsWithQU(PigTrans.java:136) at PigTrans.pigTranslator(PigTrans.java:56) at PigTrans.main(PigTrans.java:11) – user3620736 May 12 '14 at 03:09
  • I tested it and only putting one letter words in works, but putting a one letter and space makes the same error happen – user3620736 May 12 '14 at 03:19

1 Answers1

0

THe problem is with the line:

number = words [i].charAt (0);

When you put a single letter and then a space, the split method creates a 2-element array - first element is the letter and second element is an empty string. So charAt(0) obviously throw an error for the empty string!

The solution is to trim the string before feeding it to the split method.

Also, in the line:

if (word.length () <= 1)

the condition should probably be == 1 because you cannot do anything with a 0-length string.

metacubed
  • 7,031
  • 6
  • 36
  • 65