Reputation: 185
I'm confusing how to transform values in 2D char array into number (integer).
Let's assume the array is: [[a, b],[c, d],[e, f]]
or {{'a','b'},{'c','d'},{'e','f'}}
All values in that array will be converted to number, a=0, b=1, c=2, d=3, e=4, f=5.
I expect result like: [[0, 1], [2, 3], [4, 5]]
or {{0, 1},{2, 3},{4, 5}}
If it's just a string of "abcdef", I can use charAt(), but I can' use it in an array, especially in char array. So, I use .replace.
package array_learning;
public class test {
public static void main(String[] args){
char [][] word= {{'a','b'},{'c','d'},{'e','f'}};
int strLength = word.length;
for(int i = 0; i<strLength; i++){
for(int j=0; j<2; j++){
String strWord = Character.toString(word[i][j]);
strWord = strWord.replace("a","0");
strWord = strWord.replace("b","1");
strWord = strWord.replace("c","2");
strWord = strWord.replace("d","3");
strWord = strWord.replace("e","4");
strWord = strWord.replace("f","5");
System.out.print(strWord+" ");
}
System.out.println();
}
}
}
But, the result is not what I've expected.
Result:
0 1
2 3
4 5
How to solve this in the right way?
Upvotes: 1
Views: 2257
Reputation: 16359
Consider:
import java.util.Arrays;
public class Ctest {
public static void main(String[] args) {
char[][] word= { {'a', 'b'}, {'c', 'd'}, {'e', 'f'} };
println(word); // format with brackets e.g., [[a, b], [c, d]]
System.out.println(Arrays.deepToString(word)); // same format
for (int i = 0; i < word.length; i++) {
for (int j = 0; j < word[i].length; j++) {
if (word[i][j] >= 'a' && word[i][j] <= 'f') {
word[i][j] = (char) ((word[i][j] - 'a') + '0');
}
}
}
println(word); // formatted with brackets
printPlain(word); // formatted without brackets
}
public static void println(char[][] word) {
System.out.print("[");
for (int i = 0; i < word.length; i++) {
if (i > 0) System.out.print(", ");
System.out.print("[");
for (int j = 0; j < word[i].length; j++) {
if (j > 0) System.out.print(", ");
System.out.print(word[i][j]);
}
System.out.print("]");
}
System.out.println("]");
}
public static void printPlain(char[][] word) {
for (int i = 0; i < word.length; i++) {
if (i > 0) System.out.print(", ");
for (int j = 0; j < word[i].length; j++) {
if (j > 0) System.out.print(", ");
System.out.print(word[i][j]);
}
}
System.out.println();
}
}
The main changes I have made are that the values in the array are actually converted (I'm not sure if you want this; you weren't storing any new values back into the array before), the data is handled as char
without being converted to String
, the conversion is done with a calculation instead of a special case for each value, and converting the data and printing it have been separated from one another.
There are also a few minor changes. The data is now printed in the format you demonstrated, with brackets, there is no assumption that the inner arrays always have exactly two elements, and the class name has been changed to start with a capital letter.
One other minor note. On the line that converts the values from lower case letters to digits, the expression is in parentheses and is cast back to a char
. This is because when you add and subtract chars
Java performs a widening conversion to int
, so to store the value back into the char[][]
it is necessary to cast it to char
again.
I had forgotten that there is already a method in Java in the java.util.Arrays
class to format a multidimensional array with brackets: Arrays.deepToString(word)
will give you the same format as the println
method above. I had also shown a printPlain
method which is similar, but lacks the brackets, if you prefer a cleaner output format. You could also easily modify this method so that it appends to a StringBuilder
and returns a String
, instead of printing the array directly.
Upvotes: 1
Reputation: 39365
Use Arrays.toString()
to convert your array into string first. Then do the replacement. For example:
char [][] word= {{'a','b'},{'c','d'},{'e','f'}};
String strWord = "";
for(char []w : word){
// convert array int [x, y] format
strWord += Arrays.toString(w)+",";
}
// removing comma(,) from end.
strWord = strWord.replaceAll(",$", "");
// do your replacement.
strWord = strWord.replace("a","0");
// ... more
// output
System.out.println("["+strWord+"]");
Upvotes: 0
Reputation: 137
Everything is matching up correctly. This is a 2d array so your arrays are printing one by one.
If you don't want them to go on seperate lines then get rid of the System.out.println();
statement at the end of the for loop.
Upvotes: 0