为什么我得到StringIndexOutOfBoundsException?

时间:2013-02-15 13:12:53

标签: java string java.util.scanner

我被困在这个项目中。有人可以帮我吗?我收到以下错误:

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 个答案:

答案 0 :(得分:0)

你的en.ahmedali.txt文件包含什么,可能它包含一些换行符,你正在尝试使用索引使用子串,而它不包含任何,所以你必须在调用subString之前先检查字符串的长度。< / p>