[英]I can't resolve ArrayIndexOutOfBoundsException despite already submitted solutions
I have a program which effectively takes a name (eg. "John Doe") and 'festifies' it, replacing the name with a more festive name depending on the first letter of the name. 我有一个程序,该程序有效地使用一个名称(例如“ John Doe”)并将其“套用”,根据名称的首字母,用更喜庆的名称替换该名称。
My program is throwing this error: 我的程序抛出此错误:
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)
Which I can't quite seem to figure out! 我似乎还不太清楚! I understand it's doing this because my for loop is exceeding the bounds of my array, but I can't figure out how to resolve it, nor do I understand why it won't work as it should, because I think my code is correct.
我知道这样做是因为我的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();
}
}
The error occurs within my 'compareInput' method, specifically the 'if' statement. 该错误发生在我的“ compareInput”方法内,特别是在“ if”语句内。
The problem is nameInput[0]
, not alphabetLetters[i]
: 问题是
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])
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.