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.

Ads

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

Advertisements

Ads
Share on Google+Share on Google+

Java char to int

Posted on: July 23, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Java char to int  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads