2013-02-15 51 views
-3

我被困在這個項目中。有人可以幫我嗎?我收到以下錯誤:爲什麼我會得到StringIndexOutOfBoundsException?

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 
     String index out of range: 2 
    at java.lang.String.substring(Unknown Source) 
    at readFile.read(readFile.java:35) 
    at readFile.main(readFile.java:90) 

我的代碼:

import java.io.*; 

import java.util.*; 

//import java.lang.*; 

public class readFile { 

    private Scanner x; 
    public String[][] a; 
    public String temp; 
    public int surahNumber; 

    public void openFile() 
    { 
     try{ 
      x = new Scanner(new File("C:\\en.ahmedali.txt")); 
      a = new String[500][1000]; 
     } 
     catch(Exception e){ 
      System.out.println("File Not Found"); 
     } 

    } 

    public void read() 
    { 

     int i = 1; 
     int j = 1; 
     String xy,tenToninetynie,three_digit,setsurahNumber1,setsurahNumber2,setsurahNumber3; 

     while(x.hasNext()) 
     { 
      a[i][j] = x.nextLine(); 
      temp = a[i][j]; 
      //System.out.println(temp); 
      xy = temp.substring(1, 2); 

      char[] charArray = xy.toCharArray(); 
      //System.out.println(xy); 
      tenToninetynie = temp.substring(2, 3); 
      char[] charArray1 = tenToninetynie.toCharArray(); 

      three_digit = temp.substring(3, 4); 
      char[] charArray2 = three_digit.toCharArray(); 

      setsurahNumber1 = temp.substring(0, 1); 
      setsurahNumber2 = temp.substring(0, 2); 
      setsurahNumber3 = temp.substring(0, 3); 

      if(charArray[0] == '|'){ 
       surahNumber = Integer.parseInt(setsurahNumber1); 
       //System.out.println(surahNumber); 
       if(surahNumber == i+1){ 
        i++; 
        j = 0; 
       }    
      } 
      else if(charArray1[0] == '|'){ 
       surahNumber = Integer.parseInt(setsurahNumber2); 
       //System.out.println(surahNumber); 
       if(surahNumber == i+1){ 
        i++; 
        j = 0; 

       } 
      } 
      else if(charArray2[0] == '|'){ 
       surahNumber = Integer.parseInt(setsurahNumber3); 
       //System.out.println(surahNumber); 
       if(surahNumber == i+1){ 
        //System.out.println("I See"); 
        i++; 
        j = 0;    
       } 
      } 
      j++; 
      //System.out.println(j); 
      //break; 

     } 
    } 
    void write() 
    { 
     System.out.println(a[75][1]); 
    } 

    public static void main(String args[]) 
    { 
     readFile y = new readFile(); 
     y.openFile(); 
     y.read(); 
     y.write(); 
     //System.out.println(y.a[75][1]); 
    } 


} 
+1

那麼你輸入什麼值?大概只有一個字符,這意味着當你要求更多的字符時,子串*將會失敗... – 2013-02-15 13:14:58

+1

哪一行是第35行? – 2013-02-15 13:15:12

+0

xy = temp.substring(1,2); – coder 2013-02-15 13:19:55

回答

0

是什麼你en.ahmedali.txt文件包含,可能是它包含了一些斷線之後,您嘗試使用使用索引子串,儘管它不包含任何內容,所以在調用subString之前,您必須首先檢查字符串的長度。

+0

文件包含 1 | 1 | asdaskhdkj 1 | 2 | asdaszxc 1 | 3 | jxocvjxcjv 2 | 1 | jojvkcxvn 2 | 2 | joxzczxcz 3 | 1 | bcxzjicjzkxbckjzxc 這些類型的行........ – coder 2013-02-15 13:43:16

+0

1 | 1 | asdaskhdkj 1 | 2 | 1 asdaszxc | 3 | jxocvjxcjv 2 | 1 | 2 jojvkcxvn | 2 | joxzczxcz 3 | 1 | bcxzjicjzkxbckjzxc這些類型的行... – coder 2013-02-15 13:43:55

+0

如前所述,您需要在使用subString之前檢查字符串的索引是否存在,這將解決問題。 – bhupesh 2013-02-16 11:25:55

相關問題