尽管已经提交了解决方案,但我无法解决ArrayIndexOutOfBoundsException

时间:2018-12-21 21:21:30

标签: java

我有一个程序,该程序有效地取一个名字(例如“ John Doe”)并将其“标榜”,根据名字的首字母,用更喜庆的名字替换该名字。

我的程序抛出此错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at Elves.compareInput(Elves.java:38)
        at Elves.start(Elves.java:56)
        at Elves.main(Elves.java:63)

我似乎不太清楚!我知道这样做是因为我的for循环超出了数组的范围,但是我无法弄清楚如何解决它,也不知道为什么它不能按预期工作,因为我认为我的代码是正确的

import java.util.*;
public class Elves
{
    String[] festiveFirstNames = {"Peppermint","Jazzy","Peppy","Snowball","Fritz","Jingles","Bixby","Cosmo","Oodles","Zippy","Cocoa","Scout","Snickerdoodle","Buddy","Bitsy","Chippey","Pipsie","Lucky","Sugar","Chipper","Fisbee","Marshmallow","Elwood","Star","Snowflake","Twinkle"};
    String[] festiveLastNames = new String[27];
    char[] alphabetLetters = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

    public void takeInput() // Method to take user input for first and second name and pass into 'compareInput' method
    {
        Scanner sc = new Scanner(System.in);
        String firstName;
        String lastName;
        char firstNameChar;
        char lastNameChar;

        //error checking System.out.println(festiveFirstNames.length);
        //error checking System.out.println(alphabetLetters.length);
        System.out.print("Enter the first name: ");
        firstName = sc.nextLine().toUpperCase(); // Takes user input, capitalises and inserts into 'firstName' string
        firstNameChar = firstName.charAt(0); // Takes the first letter of the input and inserts into 'firstNameChar'

        System.out.print("Enter the last name: ");
        lastName = sc.nextLine().toUpperCase(); // Takes user input, capitalises and inserts into 'lastName' string
        lastNameChar = lastName.charAt(0); // Takes the first letter of the input and inserts into 'lastNameChar'

        //error checking System.out.println(firstNameChar + "\n" + lastNameChar);

        compareInput(firstNameChar, lastNameChar); // Passes two characters into the 'compareInput' method
    }

    public void compareInput(char ... nameInput)
    {
        String newFirstName = "";
        String newLastName = "";

        for (int i=0; i<alphabetLetters.length-1; i++) // Loops while 'i' less than length of alphabetLetters-1
        {
            if (alphabetLetters[i] == nameInput[0]) // If alphabet at position 'i' equals 'nameInput[0]' AKA 'firstNameChar'
            {
                newFirstName = festiveFirstNames[i]; // Update 'newFirstName' with position 'i' from 'festiveFirstNames'
            }
            //System.out.println(i);
        }

        produceOutput(newFirstName, newLastName);
    }

    public void produceOutput(String ... newNames)
    {
        System.out.println("New first name is " + newNames[0]);
    }

    public void start()
    {
        takeInput();
        compareInput();
        produceOutput();
    }

    public static void main(String[] args)
    {
        Elves e = new Elves();
        e.start();
    }
}

该错误发生在我的'compareInput'方法中,特别是'if'语句中。

1 个答案:

答案 0 :(得分:4)

问题是nameInput[0],而不是alphabetLetters[i]

public void start()
{
    takeInput();
    compareInput(); // <-- Here, you call compareInput with an EMPTY array nameInput
    produceOutput();
}

...

public void compareInput(char ... nameInput)
{
    ...

        // Here, nameInput[0] will produce an ArrayIndexOutOfBoundsException 
        // since the nameInput array is empty: 
        if (alphabetLetters[i] == nameInput[0]) 
    ...