String to 2 dimensional int array?

41 Views Asked by At

I've a combobox, I am using an Action Listener.

Inside of that function if item: 1 or 2 etc... was selected it finds that maze which is saved in a database, it points to a Java Class. I put the maze in the maze_map variable.

Here -> maze_map=connect.getMaze(which_one);

. `

comboBox.addActionListener (new ActionListener () {
  public void actionPerformed(ActionEvent e) {
    int which_one=(int) comboBox.getSelectedItem();
    String maze_map = "";// map from database
    maze_map=connect.getMaze(which_one);
    System.out.println(which_one);
    System.out.println(maze_map);       
} });

Okay So until that I'm okay, I get back a String in the maze_map variable which contains 10x10 numbers.

String maze_map which contains 10x10 numbers.

1010000000
0111010000
0001010111
0000211011
0110001001
1110000010
0010108000
1110000061
1111001010
0100111001

But, I need to put all these numbers from the maze_map into a 2 dimensional int array. My question is how to do it? I was trying with parseint but it didn't work.

1

There are 1 best solutions below

1
MRDJR97 On

Create an int array.

First you need to break the String array up into individual Strings

str = "1010000000 0111010000";
String[] splited = str.split("\\s+");

Then iterate through the array and you can use

int i=Integer.parseInt("currString"); 

to convert each String to an int.

Then just add the int to the int array.