-1

基本算法是我有一个包含多个句子的输入文件,每个句子在每一行上。我想打印从位置 s 到 e 的句子的一部分,但是使用下面的代码,它会打印出空行。有人可以告诉我我的错误在哪里?

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)
                            break;
                    }
                    for(e=i; e< lineFromFile.length(); e++)
                    {
                        if(isValidEmailCharacter(lineFromFile.charAt(e))==false)
                            break;
                    }

                    String anEmail = lineFromFile.substring(s,e);
                    System.out.println(anEmail);
                }                                             
            }
        }
        fin.close();

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

}
4

2 回答 2

0
    I try this but it doesn't work:(           
      s=i;
  while(s>-1)
  {
      if(isValidEmailCharacter(lineFromFile.charAt(s))==false)
        break;
      else
          s = 0;
      s--;
  }
  e=i;
  while(e< lineFromFile.length())
  {
      if(isValidEmailCharacter(lineFromFile.charAt(e))==false)
      break;
      else
          e = lineFromFile.length();
      e++;`enter code here`
  }
于 2013-05-19T19:41:11.620 回答
0

当您执行 s-- 时,如果您的所有字符都很好,则 for 循环可以在 s 等于 -1 时结束。同样的问题可能会影响 e++。但是对于子字符串的最后一个索引,您希望比 char 位置多一个。即字符串 cat = "Cat"; String temp = cat.substring(0, 1) 返回 C。

为了您的 s 循环检查边界,请尝试:

for(s=i;s>-1;s--)
{
if(isValidEmailCharacter(lineFromFile.charAt(s))==false)
break; 
}
if(s <0)// if it never hit break s is now == -1
s=0;

请记住循环的基础知识。如果我做

for(a=0; a<10; a++)
;

当我完成时,a 等于 10。它不会在值 10 上执行循环,因为它是 10,它使 a<10 失败,但它确实以 a=10 结束。

于 2013-05-19T19:11:41.583 回答