jeffrey zachary
jeffrey zachary

Reputation: 91

Adjacency Matrix java

User enters a text file at command-line and my prog. will take the text, create an array with the number of rows (vertices) of the first number showed, then fill the 2d array with the remaining numbers. Finally it will display if # connects to # display T, else display F. I haven't completed it, and am stuck on it just filling the array and displaying the numbers in the array.

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


class AdjMatrix {

public static void main(String[] args) {

    //ArrayList<Integer> list = new ArrayList<Integer>(); //Arraylist to store all integers in the array
    //int n = 0; //Vertices
    final int COLS = 2; //Number of columns
    int[][] array = null;
    int lineNumber = 0;
    String line = "";
    if(args.length > 0)
    {
        try
        {
            java.io.File file = new java.io.File(args[0]);
            Scanner in = new Scanner(file);

            //Reading the file
            while(in.hasNext())
                {
                line = in.next();
                lineNumber++;
                if(lineNumber == 1)
                {
                    //n = Integer.parseInt(line);
                    array = new int[Integer.parseInt(line)][COLS];
                    System.out.println(Integer.parseInt(line));
                }
                else
                {
                    String[] tokens = line.split(",");
                    for(int x = 0; x < tokens.length; ++x)
                        for(int j = 0; j < tokens.length; ++j)
                        {
                            array[x][j] = Integer.parseInt(tokens[x]);
                        }
                }

            }
            in.close();
        }//End try 
        catch(FileNotFoundException e)
        {
            System.err.println("File was either not found or it does not exist.");
            System.out.printf("\n");

        }//End catch
    }//End Commandline param entry


    for(int i = 0; i < array.length; i++)
        for(int j = 0; j < array.length; j++)
            System.out.println(" " + array[i][j]);


}
}

I put in System.out.println(Integer.parseInt(line)); to see if it grabs the number and puts it in the rows # for the array, that's successful. Any help is appreciated. Been on it for a good while and any assistance is appreciated.

EDIT Sorry, forgot to add the input file.

integer.txt

9
1,2
2,6
6,2
5,1
6,5
3,2
6,3
3,7
8,7
9,9

9 being the number that establishes the # of rows. Then the program grabs all numbers after 9

Upvotes: 1

Views: 2984

Answers (1)

Luke Willis
Luke Willis

Reputation: 8580

It looks like you're initializing a firstLine by 2 dimension array,

array = new int[Integer.parseInt(line)][COLS];

but you're trying to fill it with line.length by line.length elements.

for(int x = 0; x < tokens.length; ++x)
    for(int j = 0; j < tokens.length; ++j)
    {
        array[x][j] = Integer.parseInt(tokens[x]);
    }

This seems like a bug, but without seeing a sample file, I can't say for sure.

Upvotes: 1

Related Questions