0

So here's the deal I'm working on a project that requires me to have a 2 dimensional arraylist of 1 dimensional arrays. But every time I try to load in my data I get an error:

Can't do this opperation because of bad input java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

On some of the inputs. I've got no idea where I'm going wrong on this one. A little help please?

Source Code:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.InputStream;


public class Facebull
{


    public static void main (String[] args) {

        if(args.length != 0){
            load(args[0]);

        }
        else{
            load("testFile");
        }

    }

    public static void load(String fname) {
        int costOfMach = 0;
        ArrayList <Integer> finalMach = new ArrayList<Integer>();
        ArrayList <ArrayList<int[]>>machines = new ArrayList<ArrayList<int[]>>();

        Scanner inputFile = null;

        File f = new File(fname);

        if (f.exists ())
        {

            try
            {
                inputFile = new Scanner (f);

            }

            catch (FileNotFoundException e)
            {
                JOptionPane.showMessageDialog(null,"Can't find the file\n" + e);
            }

            int i = 0;


            while (inputFile.hasNext ( ))
            {

                String str = inputFile.nextLine ( );

                String [ ] fields = str.split ("[\t ]");

                System.out.println(str);

                if (!(fields[0].isEmpty() || fields[0].equals (""))){

                    fields[0] = fields[0].substring(1);
                    fields[1] = fields[1].substring(1);
                    fields[2] = fields[2].substring(1);

                    try
                    {   
                        //data to be inputed is 0 and 3 location of data is 1 and 2
                        int[] item = new int[2];
                        item[1] = Integer.parseInt(fields[0]);
                        item[0] = Integer.parseInt(fields[3]);

                        if(machines.size() < Integer.parseInt(fields[1])){
                            ArrayList<int[]> column = new ArrayList<int[]>();
                            machines.add (Integer.parseInt(fields[1])-1, column);
                            System.out.println("we're in the if");
                        }
                        machines.get(Integer.parseInt(fields[1])-1).add(Integer.parseInt(fields[2])-1, item);

                    }
                    //catches any exception 
                    catch (Exception e)
                    {
                        System.out.println("Can't do this opperation because of bad input \n" + e);
                    }

                }

            }
            inputFile.close ( );
        }
        System.out.print(machines);

    }//end load
}
Chris Maness
  • 37
  • 1
  • 5
  • Where is the exception being thrown? There's all kinds of places in that code it could happen. – tddmonkey Dec 21 '10 at 20:36
  • this: machines.get(Integer.parseInt(fields[1])-1).add(Integer.parseInt(fields[2])-1, item); is the code that's throwing the exception. – Chris Maness Dec 21 '10 at 20:44
  • I suggest looking at this question. http://stackoverflow.com/questions/4401850/how-to-create-multidemensional-arraylist-in-java/4401871#4401871 – Jacob Tomaw Dec 21 '10 at 20:58

2 Answers2

0

Here:

machines.add(Integer.parseInt(fields[1])-1, column);

You haven't specified a size for the machines ArrayList anywhere. ArrayList will dynamically grow when you use the one-argument version of add, since that adds to the end of the list, but when using the two-argument version, the ArrayList must already be at least as long as the index you're specifying. Since you haven't specified a size, and you haven't used the one-argument add anywhere, your ArrayList's size is 0, so no matter what index you provide, it'll be out of bounds. You can either specify a size when you create the ArrayList instance, or use ArrayList.ensureCapacity before calling add.

Then you have the same issue in the line with the get, trying to use two-argument add on an empty ArrayList.

JST
  • 1,154
  • 6
  • 15
  • Thanks for the explanation really in depth. However while I was looking at this I figured out another way that would be easier and have abandoned this method. – Chris Maness Dec 21 '10 at 20:56
0

Declare the ArrayList as shown below for a 2D ArrayList:

ArrayList<ArrayList<String>> my2DarrayList = new ArrayList<ArrayList<String>>();

Now add a new ArrayList of String at index 0:

my2DarrayList.add(new ArrayList<String>());

Now as new ArrayList is entered at index 0 so you have to add values to that particular ArrayList as shown:

my2DarrayList.get(0).add("String is added here");
Anh Pham
  • 2,108
  • 9
  • 18
  • 29