如果在使用数组时忽略语句

时间:2013-12-02 02:45:19

标签: java

我试图让我的代码从文件中读取数字,并使用.split方法将行存储到字符串数组中

之后检查该数组的元素是否等于某些字符串,但忽略了我用于此的if语句,并且以下命令仍在运行。

有人可以帮助我并告诉我为什么会这样吗?

我的代码如下,代码后面是所用文件的确切内容,如果需要,也可以重新创建文件“inrecords.dat”

package project04;

import java.util.*;
import java.io.*;
import java.lang.*;

public class Project04 
{
    static BufferedReader fin;
    static FileOutputStream fout;
    static String line = "b";
    static String line1;

    public static void fio()
    {

        String [] num = new String[1000];
        String [] num0 = new String[1];
        try
        { 
         fin = new BufferedReader(new FileReader("inrecords.dat"));

         int cnt = 0;

        try
        {
            while(cnt <= 29)
            {
                line = fin.readLine();
                num0 = line.split("");
                int ctr = line.length();
                System.out.println(line);
                System.out.println(num0[4]+num0[5]);
                cnt++;

                int a = 0;
                while(a <= (ctr-1))
                {
                    if(num0[a].equals("7") && num0[a+1].equals("3") && num0[a+2].equals("5"))
                        {
                            line1 = line;
                            System.out.println(line1);
                        }
                    a++;
                }

            }
        }

        catch (IOException | ArrayIndexOutOfBoundsException a)
        {
            System.out.println("Error 1");
        }
            try
            {
            fin.close();
            }   
            catch(IOException a)
            {
                System.out.println("Error 1a");
            }
        }

        catch (FileNotFoundException | NullPointerException e)
        {
            System.out.println("Error 2");
        }
       }

        public static void main(String[] args) 
    {
        fio();

    }

    }

档案内容“inrecords.dat”

Zimmerman, Frederick A.       (301)640-7354 
Thomas, Charles G.            (301)735-1293 
Pearl, Minnie I.              (202)586-7424 
Blackwell, Constance J.       (202)931-4412 
Payne, Isaac H.               (301)426-9982 
Mitchell, Grayson T.          (301)424-6574 
Borges, Emmanuel              (301)429-3406 
Risher, Margaret V.           (202)839-6668
Mulvahill, Margaret T.        (301)735-3345 
Lum, Ewella F.                (301)664-8591 
Gray, Ronald D.               (301)884-3506 
Plummer, Bentley M.           (301)680-5928 
Bennett, Nyra B.              (301)735-7434 
Johnston, Sharon N.           (301)648-4805 
Thomas, June D.               (202)735-5400 
Ice, Ann-Marie                (301)647-9537
Jones, MaryAnn                (301)324-5343 
Harris, Melany L.             (301)836-5113 
Gunter, Stehanie              (301)942-4056 
Hill, Carolyn G.              (301)735-3891 
McCormick, Patricia W.        (301)822-5975 
Dougherty, Shannon V.         (301)252-3250 
Knight, Nicki P.              (301)408-7357 
Moore, Valerie C.             (301)345-2023 
Edwards, Muriel A.            (301)735-8649 
Jones, Jenny J.               (301)735-0659 
Bowman, Elizabeth N.          (301)735-9535 
Goodwin, Norma H.             (301)372-3443 
Harley, Albert C.             (301)364-0512 

1 个答案:

答案 0 :(得分:0)

建议:

  1. 对于以下代码段,a >= ctr-2会有一个绑定异常的数组索引:

      while(a <= (ctr-1))
      {
        if(num0[a].equals("7") && num0[a+1].equals("3") && num0[a+2].equals("5"))
         // other code                   
      } 
    

    改为使用for循环:for(a=0; a < ctr.length-3; a++)

  2. 始终捕获异常并尝试打印堆栈跟踪或记录它:它告诉您可能有什么样的异常。打印Error 1aError 2不会告诉任何人真正发生了什么。