Home Java Javatutorial Java char to int
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java char to int

Advertisement
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. 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

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 23, 2013

Ask Questions?    Discuss: Java char to int  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com