我的程序为我提供java.lang.ArrayIndexOutOfBoundsException:1

我的程序为我提供java.lang.ArrayIndexOutOfBoundsException:1,java,Java,我正在编写一个程序,将一个短语翻译成pig拉丁语,方法是创建一个数组,数组中填充用户输入的字符串中的各个单词,然后遍历每个单词,将其转换为pig拉丁语,并将其打印到控制台。在我的方法的while循环中,通常会出现上述错误: package UpGAMING; public class Methods { public void Translator(String s, String[] index, int location) { while (index[loca

我正在编写一个程序,将一个短语翻译成pig拉丁语,方法是创建一个数组,数组中填充用户输入的字符串中的各个单词,然后遍历每个单词,将其转换为pig拉丁语,并将其打印到控制台。在我的方法的while循环中,通常会出现上述错误:

    package UpGAMING;

    public class Methods {

public void Translator(String s, String[] index, int location) {

    while (index[location].length() > 0) {

        location++;

        if(index.length > 0)s = s + index[location].substring(1) + index[location].substring(0,1).toLowerCase() + "ay";

        System.out.println(s);
    } 
}
}
如果我将条件更改为simply whiletrue,我将在此行中得到java.lang.ArrayIndexOutOfBounds:2的错误:

        if(index.length > 0)s = s + index[location].substring(1) + index[location].substring(0,1).toLowerCase() + "ay";
下面是我的类,其中包含main方法:

导入java.util.Scanner

公共课基础{

public static void main(String[] args) {

    Methods method = new Methods();
    Scanner keyboard = new Scanner(System.in);
    String input;
    int indexer = 1;
    String ss = "";

    System.out.println("Enter the word or phrase you want to translate to Pig Latin:");
    input = keyboard.next();

    String[] output = input.split("\\s+");

    method.Translator(input, output, indexer);

    System.out.println();

}
}

请提供我的代码应该是什么样子的代码块,因为我不是一个有经验的程序员。

试试这个,也许你会明白你的代码有什么问题

public class Methods {
    public void Translator(String s, String[] index, int location) {
        System.out.println("Index Length: " + index.length);
        System.out.println("Location: " + location);
        if (location >= index.length)
            System.out.println("Uppss.. ArrayIndexOutOfBoundException :((( ");
        while (index[location].length() > 0) { 
          location++; 
          if(index.length > 0) {
              s = s + index[location].substring(1) 
                    + index[location].substring(0,1).toLowerCase() 
                    + "ay";
          }
          System.out.println(s);
       } 
   }
}
试试这个

public void Translator(String s, String l, String[] index)
{
  for (String word : index) {
    String s1=s, l1=l;
    if (word.length()> 0) {
      s1 = l1 + word.substring(1) + word.substring(0, 1).toLowerCase() + "ay";
      l1 = s1;
    }
    System.out.println(s1);
  }
}

大体上,您使用Scanner.next读取输入,然后在空白处将其拆分。但是默认情况下,Scanner已经在空白处进行拆分,并且next只在输入时为您提供下一个单词。因此,我将使用循环中的next读取输入,直到您遇到约定的终止符,例如单词END。或者,您可以使用nextLine而不是next然后像你一样分开


然后在Translator中处理数组。我会采取诸如location您应该检查索引中是否存在索引位置。我认为这是您的问题。您的数组没有2个元素。谜团已解决。您可能希望打印出从keyb返回的内容oard.next;看看问题是什么。循环的退出条件是什么?空字符串?你说它没有两个元素是什么意思?我对数组不是很有经验…agim,退出条件是什么意思?那么…我如何才能使我的位置不>=index.length?谢谢你的帮助1.你将0作为位置传递n现在您通过1,您想检查第一个元素2。您应该将while循环更改为while location0和3。当我使用此策略时,您应该增加循环底部的位置…它可以工作,但不适用于多个单词…如何解决此问题?当我使用此策略时…它可以工作,但不适用于拆分为数组的字符串中有多个单词的情况。您能帮我吗?这是我的方法类的当前代码:package Up游戏;公共类方法{public void translator字符串s,字符串l,字符串[]index,int location{while location0{if index.length>0{s=l+index[location]。substring1+index[location]。substring0,1.toLowerCase+ay;l=s;location++;}System.out.printlns;}}}检查第二个方法。我更改了签名,因为我认为您不需要位置。希望这有帮助。。
public void Translator(String s, String l, String[] index)
{
  for (String word : index) {
    String s1=s, l1=l;
    if (word.length()> 0) {
      s1 = l1 + word.substring(1) + word.substring(0, 1).toLowerCase() + "ay";
      l1 = s1;
    }
    System.out.println(s1);
  }
}