0

I want to convert the 2D string array into an integer 2D array. How to do that. i tried some things but i cant concentrate right now and id like to finish it today :D Thanks in advance. So... here are all the values of the array:

parts[] []: 

[0] "25"
[1] "3"
[2] "18"

[0] "20"
[1] "12"
[2] "18"

[0] "1"
[1] "1"
[2] "15"

//string.length = 3;

String[] [] parts = new String[string.length][];
int[] [] stringToInt = new int[string.length][];

for(int a=0; a<string.length;a++){
    for(int b=0; b<3;b++){
      stringToInt[a] = Integer.parseInt(parts[a] [b]);    <---Error
    }
}
5
  • What do you think stringToInt[a] = Integer.parseInt(parts[a] [b]) does? Are you sure you didn't miss there [b] somewhere? Also what error exactly are you getting? Commented Apr 9, 2018 at 19:25
  • Please try to be more specific. What error are you getting? Can you please post a minimal reproducible example? Also this doesn't seem to be Processing code so I removed the processing tag. Commented Apr 9, 2018 at 19:26
  • "I can't concentrate right now" doesn't seem like quite the right reason to outsource this problem to us, thereby taking away our time from answering the questions of people who seriously tried and couldn't figure out the answer to a question. Commented Apr 10, 2018 at 13:44
  • Im new to Stack Overflow so i had problems with marking some passages of my code as code. Also, this IS Processing Code. What makes you think it isnt? If you mean the first lines of Code which indeed are no Code. But because as i said had problems marking those lines as non-code i marked it as code. And if i remember correctly, It was marked as code automatically and i couldnt change it because it was recognized as code. Concerning the "i cant concentrate": i thought i can ask questions when i have some? And this is a legitimate question or is it not? help me understand pls. Commented Apr 16, 2018 at 16:40
  • Sorry i answered all questions at once but i didnt see that more than one person asked a question Commented Apr 16, 2018 at 16:41

2 Answers 2

2

You have a few problems initializing the string array, and then defining the 2D int array. This is probably what you meant to do:

String parts[][] = { { "25", "3", "18" }, { "20", "12", "18" }, { "1", "1", "15" } };

int[][] stringToInt = new int[parts.length][parts[0].length];

for (int a = 0; a < parts.length; a++) {
    for (int b = 0; b < parts[0].length; b++) {
        stringToInt[a][b] = Integer.parseInt(parts[a][b]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, first of all i wanna say that the "code" on top is not really part of the code i accidentally defined it as code here. Its the output of parts [ ] [ ]. i just wanted to show how it lookes like. Sry for the misunderstanding :D And yes thanks very much. i forgot to initialize the Arrayrows...
0
    public static void main(String[] args) {
    int cols = 3;
    int rows = 3;
    String [][] parts = new String[cols][rows];

    //init the 2d array
    parts[0][0] = "25";
    parts[0][1] = "3";
    parts[0][2] = "18";

    parts[1][0] = "20";
    parts[1][1] = "12";
    parts[1][2] = "18";

    parts[2][0] = "1";
    parts[2][1] = "1";
    parts[2][2] = "15";

    //the array you want
    int [][] intArray = new int [cols][rows];

    for(int c = 0; c < cols; c++){

        for(int r = 0; r < rows; r++){
            intArray[c][r] = Integer.parseInt(parts[c][r]);
        }

    }


}

I'm thinking your error may be because you are giving stringToInt[a]= instead of stringToInt[a][b]=

1 Comment

Thanks for the submission of your comment. Yes you are right. I tried it with stringToInt[a][b]= aswell but the reason it didnt work is that i forgot to initialize the rows of the 2D array....

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.