2011-03-24 51 views
0

我得到「temp.set_account_id(Integer.parseInt(st [1] .trim()));」行上的錯誤。這裏是代碼,我不知道爲什麼它給了我一個例外。幫助將不勝感激。陣列超出索引錯誤

public static int readFile(String filename, Customer[] review)throws IOException{ 

      int count=0; 
      Scanner scan = new Scanner (new File (filename)); 

      /*Reading the first record separatly*/ 
         Customer first = new Customer(); 

         String[] a = scan.nextLine().split("="); 
         first.set_account_id(Integer.parseInt(a[1].trim())); 

         a = scan.nextLine().split("="); 
         first.set_name(a[1].toUpperCase().trim()); 

         a = scan.nextLine().split("="); 
         first.set_address(a[1].trim()); 

         a = scan.nextLine().split("="); 
         first.set_phone_number(a[1].trim()); 

         a = scan.nextLine().split("="); 
         first.set_date_of_birth(a[1].trim()); 

         a = scan.nextLine().split("="); 
         first.set_balance(Double.parseDouble(a[1].trim())); 

         review[0]= first; 
         count = count+1; 

       while (scan.hasNext()&& count>0){ 
         Customer temp = new Customer(); 
         String[] st = scan.nextLine().split("="); 

         for(int i=1;i<count;i++){ 
          if(Integer.parseInt(st[1].trim())== review[i].get_accountid()){ // checking for duplicate records 
            System.out.println("This account id is already in use so the record won't be read"); 
            for (int k=0; k<6; k++) 
             scan.nextLine(); 
          } 
          else 
           break; 
        } 


         temp.set_account_id(Integer.parseInt(st[1].trim())); 

         st = scan.nextLine().split("="); 
         temp.set_name(st[1].toUpperCase().trim()); 

         st = scan.nextLine().split("="); 
         temp.set_address(st[1].trim()); 

         st = scan.nextLine().split("="); 
         temp.set_phone_number(st[1].trim()); 

         st = scan.nextLine().split("="); 
         temp.set_date_of_birth(st[1].trim()); 

         st = scan.nextLine().split("="); 
         temp.set_balance(Double.parseDouble(st[1].trim())); 

         if (scan.hasNextLine()){ 
          scan.nextLine(); 
       } 

          int j; 
          for(j=0;j<count;j++){ 

          if (temp.get_name().compareTo(review[j].get_name())<0){ // Putting records in ascending order 
           break; 
          } 
         } 

         count=count+1; 
         for (int k=count;k>j;k--){ 
          review[k]=review[k-1]; 
         } 

         review[j]= temp; 

         if (count>=30){ 
         System.out.println("The number of records read has exceeded the limit and it will stop reading now"); 
         break; 
       } 

     } 

     System.out.println("The number of records read= " + count); 
     //System.out.println(count); 
     return count; 
    } 

回答

3

嘗試通過在行上有一個斷點來觀察變量st。 set_account_id(的Integer.parseInt(ST [1] .trim()))

這將是一個空字符串

1

最有可能在String[] a = scan.nextLine().split("=");掃描線不包含 '=' 字符。拿一個調試器,並檢查scan.nextLine()a返回的值。

+0

我發現了錯誤。輸入確實包含「=」 – dawnoflife 2011-03-24 08:15:04

0

如果scan.nextLine()將返回一個沒有=的字符串,您將得到一個長度爲1的數組。您訪問第二個元素[1],如果沒有,則會得到該異常。

0

啊我在第一條記錄中讀完後忘了繼續前進。我只是做了幾個nextline(),以便輸入字符串不會爲空。