0

I think I have the code correct for storing data in an ArrayList. However, I can't convert the list to a String[][]. Here's my code:

        int row = 0;
        int col = 0;

        String fileInput = JOptionPane.showInputDialog(null, 
                "Please enter the path of the CSV file to read:");

        File file = new File(fileInput);

        BufferedReader bufRdr;
        bufRdr = new BufferedReader(new FileReader(file));
        String line = null;

        // Construct a new empty ArrayList for type String
        ArrayList<String[]> al = new ArrayList<String[]>();

        //read each line of text file
        while((line = bufRdr.readLine()) != null) {
            String[] columnData = line.split(",");
            al.add(columnData);
        }

        // Convert ArrayList to String array
        String[][] numbers = (String[][]) al.toArray(new String[al.size()][16]);

I'm getting an error stating I can't convert to a String[][] type. Any ideas on what I can do?

EDIT: So the conversion problem is solved (the edited code is pasted above) - now its outputting nothing though. I feel like I must have stored the data incorrectly. What should I add/change?

0

1 Answer 1

2

The array will be an Object[] of String[], so the cast doesn't work. You can (and should) use the toArray() version that supplies the array to populate:

String[][] numbers = (String[][]) al.toArray(new String[al.size()][]);
Sign up to request clarification or add additional context in comments.

8 Comments

This worked, but for some reason my output is blank! Did I store the contents incorrectly?
Well, looking at your code above (I didn't before, sorry!) I see that you've used the same array object for each row; you need to create a new one. Remember that every variable of object type in Java is a reference, like a C++ pointer. You're storing the same pointer once for each row. You need to move the declaration/initialization of columnData inside the loop, so you create a new array for each row. Finally, consider using String.split() to tokenize each line; it'll give you that array directly, and StringTokenizer is deprecated.
I hate to sound needy, but being a beginner with Java, would you mind editing the code to show me what you mean? I tried moving the declaration of columnData inside the loop, but I got a null pointer exception (everything I touch creates errors!) Also, I'm not sure what to add/take out, along with where to place these additions, to use String.split(). Your help is greatly appreciated, and I apologize if I'm being a hassle.
No problem. I changed your code above. Of course, now your original question doesn't make any sense at all :)
I changed the title of the question :). I was very confident that this would finally work, but yet again I got an error (array index out of bound exception: 3). I printed out the array after generating it and the data stored seems to be where it should be, so I'm unsure why I'm getting that exception. My guess is that this is happening somewhere else in my code. The error isn't telling me what line number this is occuring on - do you know how I can retrieve that line number? That would help to see whats going on.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.