what is wrong with this code. it does not read the input file correctly.

private void readFile()
    {
String fName = "c:\\game.txt";
    ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
        {
        try         
        {
            File file = new File(fName);
            Scanner in = new Scanner(file);

//            for ( int i = 0; i < matrix.size(); i++ )
//                matrix.add(new ArrayList<String>());

            while (in.hasNextLine())
            {
                String line = in.nextLine();

                for (int i =0; i<matrix.size(); i++)
                    matrix.get(i).add(line);

            }
            in.close();
        }
        catch(Exception e)
        {
            String s = e.toString();
        }
        }
    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Comments
Third thread on same topic and you still can't use code tags properly after 121 posts.

Your question has already been answered on your other thread, and I've explained twice the mistake you are making on the code tags. Again, it's this:

[code]

// paste code here

[/code]

not this:

[/code] // paste code here

[/code]

i tried the scanner thing and nothing.

String fName = "c:\\game.txt";
    ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
        
    
   

    private void readFile()
    {
        
        try         
        {
            File file = new File(fName);
            Scanner in = new Scanner(new FileInputStream(fName)); 
            

            while (in.hasNextLine())
            {
                String line = in.nextLine();
                
                for (int i =0; i<matrix.size(); i++)
                    matrix.get(i).add(line);
            }
            in.close();
        }
        catch(Exception e)
        {
            String s = e.toString();
        }
        
    }

this is what im getting when i compile:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at Move.printFile(Move.java:87)
at Move.<init>(Move.java:40)
at Main.main(Main.java:19)
Java Result: 1

line is getting a value, but for some reason matrix is not being populated. "i" always stays at 0. and there is info on the file it is reading from

Mark your other thread solved please.

ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
matrix.get(i).add(line);

I don't see anywhere where you ever create an ArrayList of String and add it to matrix, so there's nothing to "get". You should create an ArrayList of String, add the Strings to it, then add that ArrayList to matrix. You are currently "getting" something that has never been added.

ArrayList <String> arraylist = new ArrayList<String> ();
String line;
// read a  line from file, store in line
arraylist.add (line);
matrix.add (arraylist);
Comments
A lot of patience here :)

got it working now, but it is printing out the wrong thing.

private void printFile()
    {
        
        for(int i=0; i<matrix.size(); i++)
        {
            System.out.println(matrix.get(i));
        }
    }

it is supposed to print

X XXXXX X
XXXXXXXXX
XXX X X X
X X X X X
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX

it prints
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]

Because that is the toString() representation of an ArrayList, which is what you are printing.

If you want to show it differently then iterate each list and print it as you see fit.

This article has been dead for over six months. Start a new discussion instead.