Java char to int

We are going to describe How to convert char to int. First of all we have create class name "CharToInteger" and take a char on the console.

Java char to int

We are going to describe How to convert char to int. First of all we have create class name "CharToInteger" and take a char on the console.

Java char to int

Java char to int

We are going to describe How to convert char to int. First of all we have create class name "CharToInteger" and take a char on the console. And we use java.util.Scanner Class, which call the NextLine() method. This method returns a string on a separate line. We have used charAt() method to extract characters from the string. And we use parseInt() method that converts char to int. If we parse the string value it will display a single integer value. If the given character isn't numeric, it shows a message " Not a numeric!".

Example:-

import java.util.Scanner;

public class CharToInteger {
	public static void main(String[] args) {
		try {
			Scanner scanner= new Scanner(System.in);
			 
			System.out.println("Enter a char:");
			
			String str = scanner.nextLine();
			    
			     Character character = new Character(str.charAt(0));
			     
			    System.out.println("Char: " + character);
			    
		     	String s = character.toString();
			
			      int i = Integer.parseInt(s);
			
			    System.out.println("Integer: " + i);
		
		} catch (Exception e) {
			System.out.println("Not a numeric!");
			e.getMessage();
		}
	}
}

Output

Download Source Code