-2

I'm using Eclipse and I'm trying to take a string:

1 2 3 4

out of an ArrayList:

ArrayList strings = new ArrayList<>();

And store each number into a two dimensional array:

int size = (int) strings.get(0); // Represents the number of rows
int stringList[][] = new int[size][];

// Store results in a two dimensional array
for(int i = 0; i < size; i++){
    int index = i + 1; 
    String fish = (String) strings.get(index);

    Scanner npt = new Scanner(fish);

    for(int j = 0; npt.hasNext(); j++) {
        size[i][j] = Integer.parseInt(npt.next());
    }
}

This is the section that is causing the error:

// ERROR: The type of the expression must be an array type but it resolved to int
size[i][j] = Integer.parseInt(npt.next());
Stephen
  • 23
  • 4

2 Answers2

1

strings is a raw type. Let's start by fixing that1,

List<String> strings = new ArrayList<>();

Then you can use Integer.parseInt(String) parse2 the String to an int like

int size = Intger.parseInt(strings.get(0));

Then there's no need for a cast in

String fish = (String) strings.get(index);

You can use

String fish = strings.get(index);

etc.

1 And program to the List interface.
2 Not cast.

Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You probably meant

stringList[i][j] = Integer.parseInt(npt.next());

though that wouldn't work either, unless you properly initialize the stringList array.

You would have to initialize stringList[i] (for each i) with something like stringList[i] = new String[someLength]; but I'm not sure which length you should use.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Yea, sorry. I didn't notice my error. It's so obvious now, but before you guys pointed it out I didn't see it at all. – Stephen Nov 01 '15 at 07:54