Reputation: 185
I'm trying to convert all letters in a 2D char array into number. As results, I want a = 0, b=1, c=2, ... z=25.
Then, I tried this as part of my code:
char [][] letters = {{'i', 'u'}, {'a', 'g'}, {'e', 'k'}};
for (int i = 0; i < letters.length; i++) {
for (int j = 0; j < letters[i].length; j++) {
if (letters[i][j] >= 'a' && letters[i][j] <= 'z') {
letters[i][j] = (char) ((letters[i][j] - 'a') + '0');
}
}
}
The result of my code is not what I expected before.
From 'a' to 'j', it worked well. But, from 'k' until 'z' it didn't print expected number.
What's wrong with my code?
Upvotes: 0
Views: 982
Reputation: 979
First of all you can't store the number 10 as a single char. It is two chars, 1 and 0. So the result array has to be something else than a char array. If it is int
your working with then you are probably best of storing them in an int array.
intarray[i][j] = (letters[i][j] - 'a');
where intarray is your array of int values that has the same size as the char array. Other option is to make a string array if you must store the result in the same array and convert it to a number and then into a string. Like this
String [][] letters = {{"i", "u"}, {"a", "g"}, {"e", "k"}};
for (int i = 0; i < letters.length; i++) {
for (int j = 0; j < letters[i].length; j++) {
if (letters[i][j].charAt(0) >= 'a' && letters[i][j].charAt(0) <= 'z') {
letters[i][j] = Integer.toString((letters[i][j].charAt(0) - 'a');
}
}
}
The reason is that the ascii table only has the numbers 0 to 9 as symbols. So in your case you get the first 10 letters to convert but after that you start getting various symbols in the ascii table. k for example would be :, l is ; m is < and so on. When you are working with converting chars to numbers you are better working with them as int as they are easier to do math on.
You could do the same without converting it into an int but that mean you need to find out char - 'a' is bigger than 10. If so you need get the number divided by 10 and then after that the modulo of the number and give each of them the associated ascii symbol to make them into a string. This is a lot more complex and hard to do, specially considering how easy this is by just doing it with ints.
There are 2 other options for you but I still feel the string option above is by far the easiest.
First would be to have the 2d array as an int. You could then initialize the ints to be letters. Then you can do arithmetic on them to find the number they fit. Like this:
int [][] letters = {{'i', 'u'}, {'a', 'g'}, {'e', 'k'}};
for (int i = 0; i < letters.length; i++) {
for (int j = 0; j < letters[i].length; j++) {
if (letters[i][j] >= 'a' && letters[i][j] <= 'z') {
letters[i][j] = (letters[i][j] - 'a');
}
}
}
This would have the same effect. The issue with doing it this way is that printing the actual letter takes a bit more effort and in the end you would need to convert it to a char. So I would not recommend this way.
The second option would be to store everything as the char value in a char array. Like this:
char [][] letters = {{'i', 'u'}, {'a', 'g'}, {'e', 'k'}};
for (int i = 0; i < letters.length; i++) {
for (int j = 0; j < letters[i].length; j++) {
if (letters[i][j] >= 'a' && letters[i][j] <= 'z') {
letters[i][j] = (char) (letters[i][j] - 'a');
}
}
}
To print this then you would need to cast the char to an int. So you could print them with:
for (int i = 0; i < letters.length; i++) {
for (int j = 0; j < letters[i].length; j++) {
System.out.printf("%d\n", (int) letters[i][j]);
}
}
This would then work as well but it is a bit hacky and you need to make sure you cast the char you have to an int all the time. So far I think the string array is the best option but it depends on what your goal is. But one thing that you can't do is store the char '10' in a char array as '10' isn't a single char but 2 chars and don't fit in a char.
Upvotes: 2