How To Convert Int To Char In Java
Different method to convert "Char" to "int" in Java
To convert a char to int java uses multiple ways, there is no one specific way, it tin can be done by using many methods java provides.
There are 3 methods explained in this tutorial:
- ASCII values.
- getNumericalValue.
- ParseInt using ValueOf method.
These methods are explained in the commodity
Method-one: Use ASCII values
The kickoff fashion of conversion of char to int java the char variable is assigned to an integer variable, this method returns the ASCII value of the grapheme variable. A demonstration is shown in the code below:
import java.util.*; public class principal { // main function public static void main(String[] args) { char character_variable1 = 'A'; char character_variable2 = 'B'; char character_variable3 = 'C'; char character_variable4 = 'D'; // char to int java conversion int integer_variable1 = character_variable1; // char to int coffee conversion int integer_variable2 = character_variable2; // char to int java conversion int integer_variable3 = character_variable3; // char to int java conversion int integer_variable4 = character_variable4; System.out.println("the value of integer variable i would exist the ASCII of " + character_variable1 + " i.east : " + integer_variable1); Arrangement.out.println("the value of integer variable 2 would be the ASCII of " + character_variable2 + " i.e : " + integer_variable2); Arrangement.out.println("the value of integer variable 3 would be the ASCII of " + character_variable3 + " i.e : " + integer_variable3); System.out.println("the value of integer variable iv would be the ASCII of " + character_variable4 + " i.e : " + integer_variable4); } } The output of this code is
the value of integer variable i would exist the ASCII of A i.e : 65 the value of integer variable 2 would be the ASCII of B i.e : 66 the value of integer variable iii would be the ASCII of C i.e : 67 the value of integer variable iv would be the ASCII of D i.e : 68 Besides READ: 100+ Java Interview Questions and Answers for Freshers & Experienced-1
Even if the character variable has a numerical value, its ASCII volition be displayed in this method, one mode to convert char into int coffee, when char value is numerical is to decrease 48 from them. Like in the code below:
import coffee.util.*; public class main { //main function public static void main(String[] args) { char character_variable1 = '1'; char character_variable2 = 'two'; char character_variable3 = '3'; char character_variable4 = '4'; //char to int java conversion int integer_variable1 = character_variable1 - 48; //char to int java conversion int integer_variable2 = character_variable2 - 48; //char to int java conversion int integer_variable3 = character_variable3 - 48; //char to int coffee conversion int integer_variable4 = character_variable4 - 48; System.out.println("the value of integer variable ane would be: " + integer_variable1); System.out.println("the value of integer variable ii would exist : " + integer_variable2); Organisation.out.println("the value of integer variable 3 would exist : " + integer_variable3); System.out.println("the value of integer variable 4 would be : " + integer_variable4); } } The output of the following code will be
the value of integer variable ane would exist : 1 the value of integer variable 2 would be : 2 the value of integer variable three would exist : iii the value of integer variable 4 would exist : 4 The same can be washed by subtracting char '0' from the graphic symbol asci value, the code below demonstrates how:
import java.util.*; public course primary { // main function public static void main(String[] args) { char character_variable1 = '1'; char character_variable2 = 'ii'; char character_variable3 = 'iii'; char character_variable4 = 'four'; // char to int java conversion int integer_variable1 = character_variable1 - '0'; // char to int coffee conversion int integer_variable2 = character_variable2 - '0'; // char to int java conversion int integer_variable3 = character_variable3 - '0'; // char to int java conversion int integer_variable4 = character_variable4 - '0'; System.out.println("the value of integer variable 1 would exist : " + integer_variable1); Organisation.out.println("the value of integer variable two would be : " + integer_variable2); System.out.println("the value of integer variable three would exist : " + integer_variable3); Organization.out.println("the value of integer variable iv would be : " + integer_variable4); } } The output of this code is
the value of integer variable i would be : 1 the value of integer variable two would be : 2 the value of integer variable three would be : iii the value of integer variable 4 would be : 4 Only since this method is ineffective we use grapheme.getNumericalValue() method.
ALSO READ: Java Multiline Comments [Methods & Examples]
Method-two : Character.getNumericalValue()
This method is of the grapheme class and it returns an integer value of the number assigned to a character variable. The instance is shown in the code below :
import java.util.*; public grade principal { // main function public static void main(String[] args) { char character_variable1 = 'one'; char character_variable2 = 'ii'; char character_variable3 = '3'; char character_variable4 = '4'; // utilize the method Character.getNumericValue() for char to int java conversion int integer_variable1 = Graphic symbol.getNumericValue(character_variable1); // employ the method Graphic symbol.getNumericValue() for char to int java conversion int integer_variable2 = Graphic symbol.getNumericValue(character_variable2); // utilise the method Grapheme.getNumericValue() for char to int java conversion int integer_variable3 = Character.getNumericValue(character_variable3); // employ the method Character.getNumericValue() for char to int java conversion int integer_variable4 = Graphic symbol.getNumericValue(character_variable4); Organisation.out.println("the value of integer variable ane would be : " + integer_variable1); System.out.println("the value of integer variable ii would exist : " + integer_variable2); System.out.println("the value of integer variable iii would exist : " + integer_variable3); System.out.println("the value of integer variable 4 would be : " + integer_variable4); } } The output of the post-obit code volition be
the value of integer variable ane would be : 1 the value of integer variable two would be : two the value of integer variable 3 would exist : three the value of integer variable 4 would be : 4 Method-iii : Integer.ParseInt(Cord) using String.valueOf() method
The Integer.ParseInt() method takes a string as a parameter and returns an integer value of the parameter provided, while the String.valueOf method is used to catechumen any datatype i.e. int, char, double, bladder into a string. So to use this method for the char to int java conversion, we first convert the character variable into a string, using the Cord.valueOf() method, then nosotros convert it into an integer using Integer.ParseInt(String) method. The code below demonstrates how.
import java.util.*; public class main { // master function public static void main(String[] args) { char character_variable1 = '1'; char character_variable2 = '2'; char character_variable3 = 'iii'; char character_variable4 = 'iv'; // apply the parseInt and valueOf method for char to int java conversion System.out.println("the value of integer variable 1 would be : " + Integer.parseInt(String.valueOf(character_variable1))); // use the parseInt and valueOf method for char to int coffee conversion Arrangement.out.println("the value of integer variable 2 would exist : " + Integer.parseInt(String.valueOf(character_variable2))); // use the parseInt and valueOf method for char to int java conversion System.out.println("the value of integer variable three would be : " + Integer.parseInt(String.valueOf(character_variable3))); // employ the parseInt and valueOf method for char to int java conversion System.out.println("the value of integer variable iv would be : " + Integer.parseInt(String.valueOf(character_variable4))); } } The output of the following lawmaking is
the value of integer variable 1 would be : 1 the value of integer variable two would exist : 2 the value of integer variable 3 would exist : 3 the value of integer variable 4 would be : 4 ALSO READ: Bitwise Operators in Java Explained [Applied Examples]
Practice Code
This lawmaking is to practice all the methods discussed above, dry run the lawmaking, and run information technology later
import java.util.*; public course main { // main function public static void main(String[] args) { char character_variable1 = 'i'; char character_variable2 = '2'; char character_variable3 = '3'; char character_variable4 = '4'; int integer_variable1 = Character.getNumericValue(character_variable1); int integer_variable2 = 0; int integer_variable3 = character_variable3 - 48; int integer_variable4 = character_variable4 - '0'; Organisation.out.println("the value of integer variable i would be : " + integer_variable1); Organisation.out.println("the value of integer variable 2 would be : " + Integer.parseInt(String.valueOf(character_variable2))); System.out.println("the value of integer variable 3 would be : " + integer_variable3); Arrangement.out.println("the value of integer variable 4 would be : " + integer_variable4); } } Summary
The first style of conversion of char to int coffee the char variable is assigned to an integer variable, this method returns the ASCII value of the graphic symbol variable. A sit-in is shown in the code. Fifty-fifty if the character variable has a numerical value, its ASCII will be displayed in this method, i style to catechumen char into int java, when char value is numerical is to subtract 48 from them.
Another manner is the Graphic symbol.getValue() .This method is of the graphic symbol class and information technology returns an integer value of the number assigned to a grapheme variable. The Integer.ParseInt() method takes a string every bit a parameter and returns an integer value of the parameter provided, while the Cord.valueOf method is used to convert any datatype into a string.
As well READ: Java WatchService Examples [Monitor Directory and Files]
Further Reading
To further read almost the char to int coffee conversion click on the links provided beneath:
Java Character
Java datatype conversion
Source: https://www.golinuxcloud.com/java-convert-char-to-int-examples/

0 Response to "How To Convert Int To Char In Java"
Post a Comment