数组超出索引错误

时间:2011-03-24 07:41:23

标签: java

我在行“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;
   }

4 个答案:

答案 0 :(得分:3)

尝试通过在线上有一个断点来观察变量st。 set_account_id(的Integer.parseInt(ST [1] .trim()))

这将是一个空字符串

答案 1 :(得分:1)

String[] a = scan.nextLine().split("=");中的扫描行很可能不包含'='字符。拿一个调试器并检查scan.nextLine()a返回的值。

答案 2 :(得分:0)

如果scan.nextLine()将返回不带=的字符串,则会得到长度为1的数组。您可以访问第二个元素[1],如果没有,则会得到该异常

答案 3 :(得分:0)

啊,我在第一张唱片中读完后忘了向前走。我刚刚做了几个nextline(),所以输入字符串不会为空。