I need to read in a list of adjacency matrices from a text file. I can get it to read in and write out when there is only one matrix in the file, but can't seem to get it to read through more than one. Once I tack another file onto the end of the other one, like this:

4
0 1 1 0
1 1 1 1
1 0 0 0
1 1 0 1
3
1 0 1
1 1 0 
0 1 0

...I end up with this exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at GraphPath.buildMatrix(GraphPath.java:53)
    at GraphPath.main(GraphPath.java:21)

Would you mind taking a look to see if you can help me figure out where I'm going wrong. Thanks.

chiotti

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

    public class GraphPath
   {
      static BufferedReader input;
      static PrintWriter output;
      private static int numNodes; 

      private static int matrix[][];

      public static boolean deBug = true;
      public static boolean deBug2 = false;

       public static void main(String [] args) throws IOException
      {
         GraphPath matrix = new GraphPath();
         matrix.buildMatrix();
      }  //end main

       public static void buildMatrix()throws IOException

      {
         input = new BufferedReader(new FileReader("Lab#2Input.txt"));;
         output = new PrintWriter(new FileWriter("Lab#2Output.txt"));
         String info;
         numNodes = 0;
         int rowIndex=0;

         while ((info = input.readLine()) != null)
         {
            StringTokenizer t = new StringTokenizer(info);

            if (t.countTokens() == 1)
            {
               numNodes = Integer.parseInt(info);
               matrix = new int [numNodes][numNodes];
               System.out.println ("numNodes: " + numNodes);
            }

            if (t.countTokens() >1)
            {
               int colIndex = 0;

               while (t.hasMoreTokens() && t.countTokens() <= numNodes)
               // while (t.hasMoreTokens())
               {  
                  String n = t.nextToken();
                  int m = n.charAt(0);
                  matrix[rowIndex][colIndex] = m;
                  colIndex = colIndex + 1;
               }
               rowIndex = rowIndex + 1;
            }
         }

         output.println ("The adjacency matrix is:  \n");
         if (deBug){ System.out.print ("The adjacency matrix is:    \n");}

         for (int fm = 0; fm <  numNodes; fm++)
         {   
            for (int to = 0; to < numNodes; to++)   
            {
               output.print ((char)matrix[fm][to] + "\t");   
               if (deBug){ System.out.print ((char)matrix[fm][to] + "\t");}
            }
            output.println ();  
            if (deBug){ System.out.println ();}
         }

         stringPrep();
         output.println ();  
         if (deBug){ System.out.println ();}

         //closes the PrintWriter and gives name of output file
         if (output != null)  
         {  
            output.close();             
            System.out.println("Output file has been created: Lab#2Output.txt"); 
         } 
      }

Recommended Answers

All 2 Replies

Close on the code tags:

[code=JAVA]
paste code here
[/code]

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

    public class GraphPath
   {
      static BufferedReader input;
      static PrintWriter output;
      private static int numNodes; 

      private static int matrix[][];

      public static boolean deBug = true;
      public static boolean deBug2 = false;

       public static void main(String [] args) throws IOException
      {
         GraphPath matrix = new GraphPath();
         matrix.buildMatrix();
      }  //end main

       public static void buildMatrix()throws IOException

      {
         input = new BufferedReader(new FileReader("Lab#2Input.txt"));;
         output = new PrintWriter(new FileWriter("Lab#2Output.txt"));
         String info;
         numNodes = 0;
         int rowIndex=0;

         while ((info = input.readLine()) != null)
         {
            StringTokenizer t = new StringTokenizer(info);

            if (t.countTokens() == 1)
            {
               numNodes = Integer.parseInt(info);
               matrix = new int [numNodes][numNodes];
               System.out.println ("numNodes: " + numNodes);
            }

            if (t.countTokens() >1)
            {
               int colIndex = 0;

               while (t.hasMoreTokens() && t.countTokens() <= numNodes)
               // while (t.hasMoreTokens())
               {  
                  String n = t.nextToken();
                  int m = n.charAt(0);
                  matrix[rowIndex][colIndex] = m;
                  colIndex = colIndex + 1;
               }
               rowIndex = rowIndex + 1;
            }
         }

         output.println ("The adjacency matrix is:  \n");
         if (deBug){ System.out.print ("The adjacency matrix is:    \n");}

         for (int fm = 0; fm <  numNodes; fm++)
         {   
            for (int to = 0; to < numNodes; to++)   
            {
               output.print ((char)matrix[fm][to] + "\t");   
               if (deBug){ System.out.print ((char)matrix[fm][to] + "\t");}
            }
            output.println ();  
            if (deBug){ System.out.println ();}
         }

         stringPrep();
         output.println ();  
         if (deBug){ System.out.println ();}

         //closes the PrintWriter and gives name of output file
         if (output != null)  
         {  
            output.close();             
            System.out.println("Output file has been created: Lab#2Output.txt"); 
         } 
      }

Where/when exactly are you intending to store the matrices? in other words, are you intending on reading and storing BOTH matrices at the beginning, then processing them, or do you read in one matrix, do something with it, then overwrite matrix[][] with the second matrix? If the goal is to read in all of the matrices at once in the beginning and store them all, then you'll need either a 3-D array called int matrix[][][] or some Collection of int matrix[][]. It definitely looks like you are trying to read in all of the matrices in at the beginning, so you have too much information to store in just a regular 2-D int[][].

Regardless, seems to me you should have a nested for-loop when reading data into matrix[][] opposed to a while loop. You have one later in the program AFTER you read in the data, but when reading in the data, you don' have one. Might as well have one in both places. You could design a while loop, but why, since you know exactly how many integers and lines you'll be reading in. Given that, the for-loop seems better.

Well, with this line

while (t.hasMoreTokens() && t.countTokens() <= numNodes)

the "3" that marks the start of the next "matrix" also passes the test.

Wouldn't ">1 and <= num" work better?

Edit: Also, seeing as how those single numbers declare "square" matrices, it would make even more sense to make sure that you don't read more than that to begin with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.