2014-11-24 24 views
0

嘿所以我写了一些代码来读取文本文件的内容,做一些比较并输出1或0到2D数组中。这里是一个片段使用异常处理从主要方法打印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; 


} 

我现在想输出这些来自主要方法,但每次我运行它的时候,我得到的是文本文件,并没有二维矩阵的内容。

 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()); 
    } 


} 

} 任何帮助,将不胜感激!谢谢。

+0

首先,请张贴的编译代码。 'bLine'的声明被注释掉了,但是这个变量仍然在你的代码中使用,所以这段代码不会通过编译。 – Eran 2014-11-24 16:11:48

+0

我发布的是一个片段,我不认为我需要发布我的整个代码。 bLine在其他地方初始化。我应该发布整个事情吗? – user2035796 2014-11-24 16:18:48

+0

'bLine'的初始化可能是您的问题的根源。如果它为空,你的循环会抛出NullPointerException。根据部分代码很难弄清楚你的问题。 – Eran 2014-11-24 16:21:27

回答

0

这种情况有很多问题:

if(bLine.startsWith("Visited") && equals(bLine.startsWith("Link"))) 
  1. 你永远不会初始化bLine。这意味着条件会抛出NullPointerException。我假设你想要测试从文件中读取的行。
  2. equals在这种情况下没有意义 - 它将您的readFile实例与布尔值进行比较。
  3. 一条线不能以两个前缀开头,所以你可能希望|| (OR)而不是& &(AND)。

我认为这会更有意义:

所有的
if(parseLine[i].startsWith("Visited") || parseLine[i].startsWith("Link")) 
+0

嘿,感谢您的回复。我使用“&&”的原因是因为我应该将所有以“访问”开头的行与以“链接”开头的行进行比较,以查看它们是否相同,然后在2D数组中输出1或0我该怎么做呢?谢谢 – user2035796 2014-11-24 16:43:28

+0

@ user2035796如何以x开头的行等于以y开头的行? – Eran 2014-11-24 16:46:07

+0

有一个网站列表,其中一些链接是相同的。唯一的区别是一些以“访问”为前缀,一些以“链接”为前缀。我试图将每行中的所有文本与另一行进行比较。如果一个链接与另一个链接相同,但前缀不同,我输入1其他0。我是否有意义? – user2035796 2014-11-24 16:51:20