2014-11-19 2 views
0

Excel에서 숫자를 읽고 배열에 넣으려고하는데 범위를 벗어났습니다 ... Excel 파일에 열이 2 개 있고 열이 32 개 ... 숫자가 첫 번째 열 (5 자리 길이)은 allInputs라는 배열에 삽입되는 반면 두 번째 열 (3 자리 길이) 배열은 allTargets라는 배열에 삽입됩니다. 다음은 내 코드입니다.ArrayIndexOutofBoundsException 배열

String[] allInputs = new String[32]; 
    String[] allTargets= new String[32]; 

    //Read the values from csv file 

    //Input file which needs to be parsed 
    String fileToParse = "C:\\Users\\ROBMARYAN\\Documents\\UOM\\Bsc IT Comp & Business\\3rd Yr\\Business Intelligence\\NeuralNetAssignment\\src\\inputs.csv"; 
    BufferedReader fileReader = null; 

    //Delimiter used in CSV file 
    final String DELIMITER = ","; 
    try 
    { 
     String line = ""; 
     //Create the file reader 
     fileReader = new BufferedReader(new FileReader(fileToParse)); 

     //Read the file line by line 
     int icount=0; //determine where to store the input 
     int tcount=0; //determine where to store the target 
     while ((line = fileReader.readLine()) != null) 
     { 
      //Get all tokens available in line 
      String[] tokens = line.split(DELIMITER); 
      for(String token : tokens) 
      { 
       //Print all tokens 
       if (token.length() == 5) 
       { 
         ***allInputs[icount]=token***; 
         System.out.println("Stored in all inputs:"+allInputs[icount]); 
         icount++; 
       } 
       else 
       { 
        allTargets[tcount]=token; 
        System.out.println("Stored in all targets:"+allTargets[tcount]); 
        tcount++; 
       } 
       System.out.println("iCOUNT:"+icount); 
       System.out.println("tCOUNT:"+icount); 
      } 
     } 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     try { 
      fileReader.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

여기에서 allInputs[icount]=token 인 경우 다음 오류가 발생합니다. 사전에

java.lang.ArrayIndexOutOfBoundsException: 32

감사합니다!

답변

1

셀의 콘텐츠 길이가 5 인 경우 코드 줄이 셀당 한 번 실행됩니다. 스프레드 시트가 32x2이므로 최대 64 개까지있을 수 있습니다.하지만 배열을 길이 32는 당연히 경계를 초과합니다.

+0

엑셀에 나는 5 자리 길이의 32 자리 숫자를 넣었으므로 바뀌지 않을 것입니다. 그래서 경계를 넘치지 말아야한다고 생각합니다. – user3606198

+0

OK, 그럼 파일을 보겠습니다. 우리는 이것을 재현하고 디버깅 할 수 있습니다. –

관련 문제