使用异常处理从main方法打印2D数组

时间:2014-11-24 16:07:13

标签: java arrays exception-handling io multidimensional-array

嘿所以我写了一些代码来读取文本文件的内容,进行一些比较并将1或0输出到2D数组中。这是一个片段

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

     public class readFile
     {
        private String path;
       //declare variables for visited and link
       //String inputSearch1 = "Visited";
     //String inputSearch2 = "Link";
     String word;
     public readFile(String pathname)
     {
         path = pathname;
     }

     //open the file and read it and search each line of the file for 'Visited' and 'Link'
     public String[] OpenFile() throws IOException
     {
         FileReader fr = new FileReader(path);
         BufferedReader lineReader = new BufferedReader(fr);
         int numberOfLines = readLines();
         String[] lineData = new String[numberOfLines];

         int i;
         for(i=0; i<numberOfLines; i++)
         {
            lineData[i] = lineReader.readLine();

         }
         lineReader.close();
         return lineData;
     }

    //allows the file to be parsed without knowing the exact number of lines in it
    int readLines() throws IOException
   {
      FileReader file_to_read = new FileReader(path);
      BufferedReader bf = new BufferedReader(file_to_read);

       String aLine;
      int numberOfLines = 0;
      while((aLine = bf.readLine()) != null)
      {
        numberOfLines++;
      }
     bf.close();
    return numberOfLines;       
  }

     int outLinks;
    int inLinks;
    String bLine;
   String[] searchStrings() throws IOException
{

    FileReader ftr = new FileReader(path);
    BufferedReader bf2 = new BufferedReader(ftr);
    int numberOfLines = readLines();
    //String bLine;
    String[] parseLine = new String[numberOfLines]; //array to store lines of text file
    int[][] linkMatrix = new int[outLinks][inLinks]; //2d array to store the outLinks and inLinks
    int i;
    for(i=0; i<numberOfLines; i++)
    {
        parseLine[i] = bf2.readLine();
        int j, k;
        for(j=0; j<outLinks; j++)
        {
            for(k=0; k<inLinks;k++)
            {
                if(bLine.startsWith("Visited") && equals(bLine.startsWith("Link")))
                {
                    linkMatrix[outLinks][inLinks] = 1;
                }

                else 
                {
                    linkMatrix[outLinks][inLinks] = 0;

                }System.out.println(Arrays.deepToString(linkMatrix));

            }//System.out.println();
        }
    }bf2.close();
    return parseLine;


}

我现在尝试从main方法输出这个,但每次运行它时,我得到的只是文本文件的内容而没有2D矩阵。

      import java.io.IOException;



public class linkStatistics 
{

public static void main(String[] args) throws IOException
{
    // TODO Auto-generated method stub
    //read file
    String fileName = "C:\\Users\\Ikemesit\\Documents\\Lab_4.txt";

    try
    {
        readFile file = new readFile(fileName);
        //String[] lines = file.OpenFile();
        String[] lines = file.searchStrings();
        int i;
        for(i=0;i<lines.length;i++)
        {
            System.out.println(lines[i]);
            //System.out.println(lines2[i]);

        }
    }
    catch(IOException e)
    {
        System.out.println(e.getMessage());
    }


}

} 任何帮助将非常感激!感谢。

1 个答案:

答案 0 :(得分:0)

这种情况有很多问题:

if(bLine.startsWith("Visited") && equals(bLine.startsWith("Link")))
  1. 您永远不会初始化bLine。这意味着条件会抛出NullPointerException。我假设您要测试从文件中读取的行。
  2. equals在此上下文中没有意义 - 它将您的readFile实例与布尔值进行比较。
  3. 一行不能以两个前缀开头,所以你可能想要|| (或)代替&amp;&amp; (AND)。
  4. 我认为这会更有意义:

    if(parseLine[i].startsWith("Visited") || parseLine[i].startsWith("Link"))