indexoutofboundsexception - java.lang.ArrayIndexOutOfBoundsException: 10 please assist me -


for reason when run code, java.lang.arrayindexoutofboundsexception: 10 error.

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 = "";                 {             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 have selected sadly invalid\nplease reselect 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 customers information input between 1 , 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 have selected invaild, please select 1 between 1 , 10");             infonum = 0;             addnewcustomer ();         }         system.out.println ("\nenter 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 service 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 record wish access 1 10.");         system.out.println ("or enter 11 display 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 empty");             menu ();         }         else if (filenum > 11 || filenum < 1)         {             system.out.println ("that not vaild option");             viewcustomerrecords (c);         }           else if (filenum > 0 && filenum < 11)         {             system.out.println ("customer # " + filenum + "\n");             filenum--;             c [filenum].print ();             menu ();         }           if (filenum == 11)         {             (int = 0 ; < 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 ();         }     } } 

there bug in viewcustomerrecords. allow "11" option view customers. 10 (i.e. 11 minus 1) not valid index c[], goes 0 9, checking if record c[10] null before checking else.

for matter, think let type in above 11 well, , cause exception.

rearranging tests fix particular problem:

    if (filenum > 11 || filenum < 1)     {         system.out.println ("that not valid option");         viewcustomerrecords (c);     }     else if (filenum > 0 && filenum < 11)     {         if (c [filenum - 1] == null)         {             system.out.println ("this record empty");             menu ();         }         else         {             system.out.println ("customer # " + filenum + "\n");             c [filenum - 1].print ();             menu ();         }     }     else     {         // . . . show customers . . . 

if got arrayindexoutofboundsexception exception when typing in "11" problem reported. otherwise, maybe have 2 problems. :-)


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -