简体   繁体   English

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

[英]Printing a 2D array from the main method using exception handling

Hey so I've written some code to read the contents of a text file, do some comparisons and output either 1 or 0 into a 2D array. 嘿,所以我写了一些代码来读取文本文件的内容,进行一些比较,然后将1或0输出到2D数组中。 Here is a snippet 这是一个片段

     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;


}

I am now trying to output this from the main method but each time I run it, all I get is the contents of the text file and no 2D matrix. 我现在正尝试从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());
    }


}

} Any help would be much appreciated! } 任何帮助将非常感激! Thanks. 谢谢。

This condition has many problems : 这种情况有很多问题:

if(bLine.startsWith("Visited") && equals(bLine.startsWith("Link")))
  1. You never initialize bLine . 您永远不会初始化bLine This means that the condition would throws NullPointerException . 这意味着条件将抛出NullPointerException I'm assuming you want to test the lines you read from the file instead. 我假设您想测试从文件读取的行。
  2. equals makes no sense in this context - it compares your readFile instance with a boolean. 在这种情况下, equals没有意义-它会将您的readFile实例与一个布尔值进行比较。
  3. A line can't start with both prefixes, so you probably want || 一行不能以两个前缀开头,因此您可能需要||。 (OR) instead of && (AND). (OR)代替&&(AND)。

I think this would make more sense : 我认为这会更有意义:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM