0

我收到一个错误:StringIndex out of range: -1 with error line is String anEmail = lineFromFile.substring(s+1, e)。如您所见,我试图在输入文件中打印一行的一部分,但我不起作用。有人可以帮我解释为什么吗?

import java.io.*;

public class Email13 
{
    static boolean isValidEmailCharacter(char c)
    {
        boolean result = false;
        if((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||(c=='.')||(c=='-')||(c=='+'))
            result = true;
        return result;
    }

    public static void main(String[] args) throws Exception{
    BufferedReader cin, fin;
    cin = new BufferedReader(new InputStreamReader(System.in));
    //Description
    System.out.println("Programmer: Minh Nguyen");
    System.out.println("Description: This program is to start the final project.");
    System.out.println();    

    String nameIn, nameOut, deIn, deOut;
    nameIn="";
    nameOut="";
    deIn = "fileContainingEmails.txt";

    System.out.print("Enter input filename [default:" + deIn + "]: ");
    nameIn = cin.readLine();

    if(nameIn.compareTo("")==0){
        nameIn = deIn;
      deOut = "copyPasteMyEmails.txt";
        System.out.print("Enter output filename [default:" + deOut + "]: ");
        nameOut = cin.readLine();
        if(nameOut.compareTo("")==0)
          nameOut = deOut;
    }
    else if(nameIn.compareTo("")>0){
        deOut = nameIn;
        System.out.print("Enter output filename [default:" + deOut + "]: ");
        nameOut = cin.readLine();
        if(nameOut.compareTo("")==0)
          nameOut = nameIn;
    }
    fin = new BufferedReader(new FileReader(nameIn));
    //Read the input file
    while(true)
    {
        if(!fin.ready()) break;
        String lineFromFile;
        lineFromFile = fin.readLine();
        int s, e, hasDot;

        for (int i = 0; i < lineFromFile.length(); i++) // for each char in the string...           
        {
      if(lineFromFile.charAt(i)=='@')
      {
          for(s=i;s>-1;s--)
          {
              if(isValidEmailCharacter(lineFromFile.charAt(s))==false)
                  for(e=1; e< lineFromFile.length(); e++)
                  {
                      if(isValidEmailCharacter(lineFromFile.charAt(e))==false)
                      {
                          String anEmail = lineFromFile.substring(s+1, e);
                          System.out.println(anEmail);
                          break;
                      }

                  }
          }



      }                                                               

      }

  }
     fin.close();

    PrintWriter fout;
    fout = new PrintWriter(new FileWriter(nameOut));
    fout.close();        
  }

}
4

2 回答 2

1

由于数组是从零开始的索引,因此您lineFromFile.substring(s+1, e)会抛出错误。

for(s=i;s>-1;s--){
    //lines of code
    String anEmail = lineFromFile.substring(s+1, e)

这将失败i = lineFromFile.length

因为它会转化为最后一个元素lineFromFile[i+1]在哪里。lineFromFile[i]

于 2013-05-19T03:27:11.190 回答
1

假设一行有 10 个字符。所以i循环从0到9。

然后你有一个s循环,从i0 开始。

在其中,您可以访问 `linefromFile.subString(s+1);

所以当i是 9 时,您的s循环从 9 开始,并且您尝试访问索引 9+1,即索引 10,它在您的行之外。

于 2013-05-19T03:28:42.997 回答