+ 2

Casting from a char to int

Never work this code no matter what I use It does not work import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); char a = read.next().charAt(0); // int c= Integer.toString(a).charAt(0); // int c = Integer.parseInt(a); // int c = (int) a.charAt(0); // int c = (char) (a+1); // int c = Character.getNumericValue(a); int c = Integer.parseInt(String.valueOf(a)); //int c = a; // a = (c); int c = (int) a.charValue() System.out.println(c); //your code goes here }

10th Apr 2021, 11:33 AM
Sana Omar
Sana Omar - avatar
2 Answers
+ 1
char a = '3'; char b = '4'; int c = Character.getNumericValue(a); int d = Character.getNumericValue(b); int r = c + d; System.out.println(r);
10th Apr 2021, 1:01 PM
Coding Cat
Coding Cat - avatar
0
In Java, we can convert the Char to Int using different approaches. If we direct assign char variable to int, it will return the ASCII value of a given character. https://www.pfmlogin.org/ If the char variable contains an int value, we can get the int value by calling Character. getNumericValue(char) method.
27th Dec 2022, 9:05 AM
22Bickford