简体   繁体   中英

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. 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.

      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 . This means that the condition would throws 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.
  3. A line can't start with both prefixes, so you probably want || (OR) instead of && (AND).

I think this would make more sense :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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