Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组索引超出范围异常4_Java_Arrays_Indexoutofboundsexception - Fatal编程技术网

Java 数组索引超出范围异常4

Java 数组索引超出范围异常4,java,arrays,indexoutofboundsexception,Java,Arrays,Indexoutofboundsexception,当我试图删除或显示我创建的用户时,总是会出现错误。指向它实际删除或显示某个用户的代码行。我似乎不知道错误是从哪里来的 import java.util.*; public class contactInfo { public static void main(String[] args) { String tracker[][] = { {" ", " ", " ", " ",}, {" ", " ", "

当我试图删除或显示我创建的用户时,总是会出现错误。指向它实际删除或显示某个用户的代码行。我似乎不知道错误是从哪里来的

import java.util.*;
public class contactInfo {
    public static void main(String[] args) {
        String tracker[][] = {
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",}
        };

        Scanner input = new Scanner(System.in);
        int picker = 0;

        while (true) {
            System.out.print("Please choose an option: \n 1. Add a user  \n 2. Delete a user  \n 3. Display a user  \n 4. Quit ");
            picker = input.nextInt();
            if (picker == 1) {
                addUser(tracker);
            } else if (picker == 2) {
                deleteUser(tracker);
            } else if (picker == 3) {
                displayUser(tracker);
            } else {
                break;
            }
        }
    }

    public static String[][] addUser(String[][] add) {
        Scanner input = new Scanner(System.in);

        System.out.print("Which user is this information for (1 - 10): ");
        int user = input.nextInt();
        System.out.println(" ");

        System.out.print("Enter the users first name: ");
        add[user][0] = input.next();
        System.out.println(" ");

        System.out.print("Enter the users last name: ");
        add[user][1] = input.next();
        System.out.println(" ");

        System.out.print("Enter the users phone number (without dashes): ");
        add[user][2] = input.next();
        System.out.println(" ");

        System.out.print("Enter the users age: ");
        add[user][3] = input.next();
        System.out.println(" ");

        return add;
    }

    public static String[][] deleteUser(String[][] del) {
        Scanner input = new Scanner(System.in);

        System.out.print("Which user would you like to delete?: ");
        int user = input.nextInt();

        for (int i = 0; i < del.length - 1; i++) {
            del[user][i] = " ";
        }

        return del;
    }

    public static String[][] displayUser(String[][] displ) {
        Scanner input = new Scanner(System.in);

        System.out.print("Which user would you like to display?: ");
        int user = input.nextInt();

        for (int i = 0; i < displ.length; i++) {
            System.out.print(displ[user][i] + " ");
        }

        return displ;
    }
}
import java.util.*;
公共类联系人信息{
公共静态void main(字符串[]args){
字符串跟踪器[][]={
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",}
};
扫描仪输入=新扫描仪(System.in);
int-picker=0;
while(true){
System.out.print(“请选择一个选项:\n 1.添加用户\n 2.删除用户\n 3.显示用户\n 4.退出”);
picker=input.nextInt();
如果(选择器==1){
addUser(跟踪器);
}else if(选择器==2){
删除用户(跟踪器);
}否则如果(选择器==3){
显示用户(跟踪器);
}否则{
打破
}
}
}
公共静态字符串[][]addUser(字符串[][]add){
扫描仪输入=新扫描仪(System.in);
System.out.print(“此信息用于(1-10)哪个用户):”;
int user=input.nextInt();
System.out.println(“”);
System.out.print(“输入用户名:”);
添加[user][0]=input.next();
System.out.println(“”);
System.out.print(“输入用户姓氏:”);
添加[user][1]=input.next();
System.out.println(“”);
System.out.print(“输入用户电话号码(不带破折号):”;
添加[user][2]=input.next();
System.out.println(“”);
System.out.print(“输入用户年龄:”);
添加[user][3]=input.next();
System.out.println(“”);
返回添加;
}
公共静态字符串[][]删除用户(字符串[][]删除){
扫描仪输入=新扫描仪(System.in);
System.out.print(“您要删除哪个用户?:”;
int user=input.nextInt();
对于(int i=0;i
您的跟踪器是一个二维阵列。10行,每行4个字符串。问题在于此代码:

    for (int i = 0; i < del.length - 1; i++)
    {
        del[user][i] = " ";
    }
for(int i=0;i
迭代4个字符串的数组,但变量i将从0变为8。您想要的是:

    for (int i = 0; i < del[user].length; i++)
    {
        del[user][i] = " ";
    }
for(int i=0;i
请使用disf[user].length或del[user].length,而不是使用disf.length或del.length进行限制

用户正在输入“user”变量,您需要一个值1-10,并将其用作数组的索引。数组在Java中是基于零的索引。因此,要么从用户输入中减去一,要么请求0-9。此外,您应该检查您收到的索引是否