2

How can I convert the ints in a 2d array into chars, and strings? (seperately)

If I copy ints to a char array i just get the ASCII code.

For example:

public int a[5][5] 

//some code

public String b[5][5] = public int a[5][5]

Thanks

3
  • 4
    Your question isn't clear. Please give sample input and required output data. Commented Oct 27, 2010 at 13:43
  • You can have only one char in cell. How do you want to make strings from it? Which direction? Commented Oct 27, 2010 at 13:55
  • just instead of having an int in a cell, replace it with a char(or string). E.G. [0][0] = 5 as an int converted to [0][0] = 5 as a char. Justing replacing the int values with the char or string equivalent. I am hoping to replicate the int array as a char array and a string array. Commented Oct 27, 2010 at 13:57

5 Answers 5

2

This question is not very well-phrased at all. I THINK what you're asking is how to convert a two-level array of type int[][] to one of type String[][].

Quite frankly, the easiest approach would simply leave your array as-is... and convert int values to String's when you use them:

Integer.toString(a[5][5]);

Alternatively, you could start with a String[][] array in the first place, and simply convert your int values to String when adding them:

a[5][5] = new String(myInt);

If you really do need to convert an array of type int[][] to one of type String[][], you would have to do so manually with a two-layer for() loop:

String[][] converted = new String[a.length][];
for(int index = 0; index < a.length; index++) {
    converted[index] = new String[a[index].length];
    for(int subIndex = 0; subIndex < a[index].length; subIndex++){
        converted[index][subIndex] = Integer.toString(a[index][subIndex]);
    }
}

All three of these approaches would work equally well for conversion to type char rather than String.

Sign up to request clarification or add additional context in comments.

Comments

0

Your code must basically go through your array and transform each int value into a String. You can do this with the String.toString(int) method.

You can try that :

String[][] stringArray = new String[a.length][];
for(int i = 0; i < a.length; i++){
    stringArray[i] = new String[a[i].lenght];
    for(int j = 0; j < a[i].length; j++){
        stringArray[i][j] = Integer.toString(a[i][j]);
    }
}

Comments

0

If you want the int number as a string then you can use the Integer.toString() function.

b[1][1] = Integer.toString(a[1][1]);

Comments

0
String [][]b = new String[a.length][];
for(int i=0; i<a.length; i++) {
  int [] row = a[i];
  b[i] = new String[row.length];
  for(int j=0; j<row.length; j++) {
    b[i][j] = Integer.toString(row[j]);
  }
}

Comments

-1

To convert a 2D array into String you can use Arrays.deepToString(stringArr).

Comments

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.