Java 1个字母单词,带Pig拉丁语编程

Java 1个字母单词,带Pig拉丁语编程,java,apache-pig,latin,Java,Apache Pig,Latin,每当我尝试执行这段代码时,每当我输入一个单字母单词时,它都会返回一个错误,尽管编译过程很好,并且使用任何其他单词执行一个多字母的短语\ 我不明白为什么会这样,因为一切都应该正常返回,但只要代码经过一个字母单词,它就会自动停止程序并结束它 import javax.swing.*; // The "PigTrans" class. public class PigTrans { public static void main (String[] args) { String userIn

每当我尝试执行这段代码时,每当我输入一个单字母单词时,它都会返回一个错误,尽管编译过程很好,并且使用任何其他单词执行一个多字母的短语\

我不明白为什么会这样,因为一切都应该正常返回,但只要代码经过一个字母单词,它就会自动停止程序并结束它

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
import javax.swing.*;
//“PigTrans”类。
公营皮格特
{
公共静态void main(字符串[]args)
{
字符串用户输入;
userInput=JOptionPane.showInputDialog(null,“请输入单词”);
System.out.println(PigTrans.pigtranser(用户输入));
}//主方法
公共静态字符串转换器(字符串字)
{
国际[]无秩序;
字符串短语=”;
String[]words=word.split(\\s+);
String[]newWord=新字符串[words.length];
for(int i=0;i如果(word.length()=48)和(ascii=65)和(ascii,则问题在于行:

number = words [i].charAt (0);
if (word.length () <= 1)
当您先放置一个字母,然后放置一个空格时,
split
方法会创建一个2元素数组-第一个元素是字母,第二个元素是空字符串。因此charAt(0)显然会为空字符串抛出一个错误

解决方案是在将字符串馈送到
split
方法之前
trim
字符串。

此外,在该行中:

number = words [i].charAt (0);
if (word.length () <= 1)

if(word.length())您看到了什么错误消息?哪一行引发了异常?java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:2位于java.lang.String.substring(未知源代码)位于PigTrans.startsWithQU(PigTrans.java:136)位于PigTrans.PigTrans.main处的PigTrans.PigTrans(PigTrans.java:56)(PigTrans.java:11)我对它进行了测试,只在works中输入了一个字母单词,但输入一个字母和空格会导致同样的错误