+ 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 }
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);
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.
Hot today
What's wrong? :(
1 Votes
Why won't my image display
0 Votes
I have made a calculator in which my % (Percentage) not work correctly for 100%50 or 100%20.
2 Votes
How the answer is 50?
0 Votes
Number of Ones ( C++ ) question!
1 Votes