Java – applet – how do I use this code using strings instead of characters?
•
Java
I'm slowly trying to write a program that converts hexadecimal numbers to decimal numbers I'm not interested in reading the completed, well-known code because I want to do it myself I have an idea, but something interferes with me
import java.util.Scanner; public class Test{ public static void main(String[] args){ Scanner input = new Scanner(system.in); String any = input.nextLine(); char[] cArray = any.tocharArray(); for(int i=0; i<cArray.length; i++){ System.out.print(cArray[i]+" "); } } } Input: ab12 Output: a b 1 2
I want to replace a with 10, B with 11, C with 12, etc
If I add an IF statement to the for loop, it works
for(int i=0; i<cArray.length; i++){ if(cArray[i] == 'a'){ cArray[i] = '10'; // doesn't work,read below } System.out.print(cArray[i]+" "); }
The problem is that I want to replace 10 and 10 is no longer a character because it consists of two letters That's why I want to know how to use strings instead of characters to use this code?
Solution
hint
'a' - 87 = 10
So you can use:
(int) cArray[i] - 87
Because:
(int)'a' = 97
I hope you understand the idea
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码