Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.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.lang.ArrayIndexOutofBounds异常:10请帮助我_Java_Indexoutofboundsexception - Fatal编程技术网

java.lang.ArrayIndexOutofBounds异常:10请帮助我

java.lang.ArrayIndexOutofBounds异常:10请帮助我,java,indexoutofboundsexception,Java,Indexoutofboundsexception,由于某种原因,当我运行这段代码时,我得到java.lang.ArrayIndexOutOfBoundsException:10错误 public class customerRecords { static Customer[] c = new Customer [10]; public static void main (String[] args) throws Exception { menu (); } public s

由于某种原因,当我运行这段代码时,我得到java.lang.ArrayIndexOutOfBoundsException:10错误

public class customerRecords
    {
    static Customer[] c = new Customer [10];
    public static void main (String[] args) throws Exception
    {
        menu ();
    }


    public static void menu () throws Exception
    {
        int option = 0;
        String blank = "";
        do
        {
            String stroption = "";
            char choption = ' ';
            option = 0;
            System.out.println ("Main Menu\n1. Add New Customer Information\n2. View customer     Records \n3. Exit");
            System.out.println ("\n");
            choption = (char) System.in.read ();
            while (choption >= '0' && choption <= '9')
            {
                stroption += choption;
                choption = (char) System.in.read ();
            }
            option = Integer.parseInt (stroption);
            System.in.skip (1);

            if (option == 1)
            {
                addNewCustomer ();
                blank = "";
                option = -1;
            }
            else if (option == 2)
            {
                viewCustomerRecords (c);
                blank = "";
                option = -1;

            }
            else if (option == 3)
            {
                System.out.print ("\nGood Bye");
                System.exit (0);

            }


            else
            {
                System.out.println ("The option that you have selected is sadly Invalid\nPlease reselect a new option");
                blank = "";
                option = -1;
            }

        }
        while (option < 1 || option > 3);
    }


    public static void addNewCustomer () throws Exception
    {

        String name = "", phoneNumber = "", address = "", choice = "", strinfo = "", strnum = "";
        char chname = ' ', chphone = ' ', chaddress = ' ', chinfo = ' ', chnum = ' ';
        int num = 0, infonum = 0, phonenum = 0;
        System.out.println ("\nEnter which customers information you would like to input between 1 and 10");
        chinfo = (char) System.in.read ();
        while (chinfo >= '0' && chinfo <= '9')
        {
            strinfo += chinfo;
            chinfo = (char) System.in.read ();
        }
        infonum = Integer.parseInt (strinfo);
        System.in.skip (1);
        if (infonum > 10 || infonum < 1)
        {
            System.out.println ("The option that you have selected is invaild, please select one between 1 and 10");
            infonum = 0;
            addNewCustomer ();
        }
        System.out.println ("\nEnter the customer " + (infonum) + "'s name");
        chname = (char) System.in.read ();
        while ((chname >= 'A' && chname <= 'Z') || (chname >= 'a' && chname <= 'z') || (chname == ' '))
        {
            name += chname;
            chname = (char) System.in.read ();
        }
        System.in.skip (1);

        System.out.println ("Enter customer " + (infonum) + "'s phone number");
        chphone = (char) System.in.read ();
        while ((chphone >= '0' && chphone <= '9') || (chphone == '-') || (chphone == ' '))
        {
            phoneNumber += chphone;
            chphone = (char) System.in.read ();
        }
        System.in.skip (1);

        System.out.println ("Enter customer " + (infonum) + "'s address");
        chaddress = (char) System.in.read ();
        while ((chaddress >= '0' && chaddress <= '9') || (chaddress >= 'A' && chaddress <= 'Z') || (chaddress >= 'a' && chaddress <= 'z') || (chaddress == ' '))
        {
            address += chaddress;
            chaddress = (char) System.in.read ();
        }
        System.in.skip (1);

        System.out.println ("Enter which service you would like to purchase. 1 = DialUp, 2 = Portable, 3 = Cable");
        chnum = (char) System.in.read ();
        while (chnum >= '0' && chnum <= '9')
        {
            strnum += chnum;
            chnum = (char) System.in.read ();
        }
        System.in.skip (1);
        num = Integer.parseInt (strnum);
        if (num == 1)
        {
            c [infonum - 1] = new DialUp (name, phoneNumber, address, 18.99);
        }
        else if (num == 2)
        {
            c [infonum - 1] = new Portable (name, phoneNumber, address, 29.95, 5);
        }
        else if (num == 3)
        {
            c [infonum - 1] = new Cable (name, phoneNumber, address, 46.99, 4, 7);
        }
        else
        {
            System.out.println ("Invaild");

        }
        System.out.println ("\n");
        menu ();

    }



    public static void viewCustomerRecords (Customer[] c) throws Exception
    {
        String file = "";
        char chfile = ' ';
        int filenum = 0;


        System.out.println ("Enter the record that you wish to access from 1 to 10.");
        System.out.println ("Or enter 11 to display all files");

        chfile = (char) System.in.read ();
        while (chfile >= '0' && chfile <= '9')
        {
            file += chfile;
            chfile = (char) System.in.read ();
        }
        System.in.skip (1);
        filenum = Integer.parseInt (file);

        if (c [filenum - 1] == null)
        {
            System.out.println ("This record is empty");
            menu ();
        }
        else if (filenum > 11 || filenum < 1)
        {
            System.out.println ("That is not a vaild option");
            viewCustomerRecords (c);
        }


        else if (filenum > 0 && filenum < 11)
        {
            System.out.println ("Customer # " + filenum + "\n");
            filenum--;
            c [filenum].print ();
            menu ();
        }


        if (filenum == 11)
        {
            for (int i = 0 ; i < c.length ; i++)
            {
                if (c [i] == null)
                {
                    System.out.println ("File #" + (i + 1) + "is empty");
                }
                else
                {
                    System.out.println ("\nCustomer # " + (i + 1) + "\n");
                    c [i].print ();
                }
            }
            menu ();
        }
    }
}

viewCustomerRecords中存在错误。您允许11作为查看所有客户的选项。显然,10即11减去1不是c[]的有效索引,c[]只从0到9,但在检查任何其他内容之前,您正在检查记录c[10]是否为空

就这一点而言,我认为它将允许您输入11以上的任何内容,这也将导致一个异常

重新安排测试将解决此特定问题:

    if (filenum > 11 || filenum < 1)
    {
        System.out.println ("That is not a valid option");
        viewCustomerRecords (c);
    }
    else if (filenum > 0 && filenum < 11)
    {
        if (c [filenum - 1] == null)
        {
            System.out.println ("This record is empty");
            menu ();
        }
        else
        {
            System.out.println ("Customer # " + filenum + "\n");
            c [filenum - 1].print ();
            menu ();
        }
    }
    else
    {
        // . . . show all customers . . .

如果键入11时出现ArrayIndexOutOfBoundsException异常,那么几乎可以肯定这就是您报告的问题。否则,您可能有两个问题:-

您想让我们查看整个代码以找出异常发生的位置吗?stacktrace在哪里?请编辑问题以包含整个错误消息。这是学习如何调试的好机会。实际查看异常消息和堆栈跟踪。如果你真的花时间去看的话,他们会告诉你问题的确切位置,以及导致问题的原因。