2014-12-05 2 views
0

이 코드를 실행할 때 어떤 이유로 java.lang.ArrayIndexOutOfBoundsException : 10 오류가 발생합니다.java.lang.ArrayIndexOutOfBoundsException : 10 please please me

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(); 
     } 
    } 
} 
+0

예외가 발생한 곳을 찾기 위해 전체 코드로 이동하기를 원하십니까? 스택 트레이스는 어디에 있습니까? – Ouney

+0

질문을 편집하여 전체 오류 메시지를 포함하십시오. – tbodt

+0

그리고 이것은 디버깅하는 법을 배울 수있는 좋은 기회입니다. 사실 예외 메시지와 스택 추적을 확인하십시오. 그들은 실제로 문제가있는 곳과 그 원인에 대해 많은 것을 알려줍니다. 실제로 LOOK에 시간을 할애해야합니다. –

답변

0

viewCustomerRecords에 버그가 있습니다. 모든 고객을 볼 수있는 옵션으로 "11"을 허용합니다. 분명히 10 (즉, 11 빼기 -1)은 0에서 9까지만가는 c []에 유효한 색인이 아니지만 다른 것을 확인하기 전에 레코드 c [10]이 null인지 확인하고 있습니다.

그 점에 대해서는 11 세 이상에서도 타이핑 할 수 있으며 예외가 발생할 수 있다고 생각합니다.

테스트를 정리하는이 특별한 문제가 해결됩니다 : "11"을 입력 할 때 다음이 신고 문제가 거의 확실하다는 ArrayIndexOutOfBoundsException 예외를 가지고있는 경우

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 . . . 

합니다. 그렇지 않으면 어쩌면 두 가지 문제점이있을 수 있습니다. :-)

관련 문제