Java 二维阵列';标准普尔;越界例外
我讨厌数组 所以我一直在做一些编码,我发现了一个错误(越界异常),我似乎无法修复。我相信我说的‘array1[counter 2][counter]=input2.nextLine();’是问题,但我不知道是什么问题!救命啊,我受不了这些越界的例外 该计划的想法是一个在线电话簿,你可以添加联系人,查看他们,并按他们的名字,姓氏和电话号码搜索 以下是我使用的代码:Java 二维阵列';标准普尔;越界例外,java,arrays,multidimensional-array,indexoutofboundsexception,Java,Arrays,Multidimensional Array,Indexoutofboundsexception,我讨厌数组 所以我一直在做一些编码,我发现了一个错误(越界异常),我似乎无法修复。我相信我说的‘array1[counter 2][counter]=input2.nextLine();’是问题,但我不知道是什么问题!救命啊,我受不了这些越界的例外 该计划的想法是一个在线电话簿,你可以添加联系人,查看他们,并按他们的名字,姓氏和电话号码搜索 以下是我使用的代码: import java.util.Scanner; import java.awt.*; public class testMatt
import java.util.Scanner;
import java.awt.*;
public class testMattWalker {
//
public static void main (String[] args){
//Declare all your variables here. Make sure to provide a comment explaining the purpose of each variable
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
Scanner input3 = new Scanner(System.in);
Scanner input4 = new Scanner(System.in);
int counter = 0;
int counter2 = 0;
boolean go = true;
//Temp VAriables for entry
String firstNameOfEntry = "";
String lastNameOfEntry = "";
String personPhoneNumber = "";
//
//create array
String [][] array1 = new String[5][3];
while (go) {
String choice = "";
System.err.println("\n\n\n\n\n\n\n\n\nDIDGITAL PHONE BOOK 2013");
System.out.println("1- Create phone book\n2- Display phone book\n3- Find person(s) by last name\n4- Find person(s) by first name\n5- Find person(s) by phone number\n6- Exit application");
choice = input.nextLine();
if (choice.equals("1") && counter2 != 6) {
System.err.println("\n\n\n\n\nPHONE BOOK ENTRY CREATOR:");
System.out.println("Please enter the first name of the person you wish to enter: ");
array1[counter2][counter] = input2.nextLine();
counter++;
System.out.println("Please enter the last name of the person you wish to enter: ");
array1[counter2][counter] = input3.nextLine();
counter++;
System.out.println("Please enter the phone number of this person: example:9057773344");
array1[counter2][counter] = input4.nextLine();
counter++;
counter2++;
}else if (choice.equals("2")) {
}else if (choice.equals("3")) {
}else if (choice.equals("4")) {
}else if (choice.equals("5")) {
}else if (choice.equals("6")) {
}
}
}// end of main
}// end of class
我知道这还没完成,但我是那种喜欢在继续前进之前修复一切的人,所以任何帮助都会被感激!(:请记住,数组索引以
0
开头。因此:5
已经超出了计数器2的界限。您将数组的第二维度设置为3,但在代码中,您将1添加到计数器3次,这意味着它在代码的第一次迭代后超出了数组的界限
正如ljgw所说,数组索引从0开始,因此维度为3意味着对应的索引是0、1和2