Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-conversion Convert Character into Integer

Related Tutorials


 
 

Share on Google+Share on Google+

Convert Character into Integer

Advertisement
In this section, you will learn to convert the character into a integer. The java.lang package convert provides the facility to convert the character data into an integer type.

Convert Character into Integer

     

In this section, you will learn to convert the character into a integer. The java.lang package convert provides the facility to convert the character data into an integer type.

Description of code:

This program helps you to convert the character data into an integer type data. It defines a class name "CharToInt" and takes a character on the console. The given character is converted into an integer using the parseInt() method. It parses the string argument as a signed Integer value. Whenever the given character isn't  numeric, it displays a message " Not a numeric!".

Here is the code of this program:

import java.io.*;
import java.lang.*;

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

Download this program:

Output of this program.

C:\vinod\xml\convert>javac CharToInt.java

C:\vinod\xml\convert>java CharToInt
Enter a char:
S
Char: S
Not a numeric!

C:\vinod\xml\convert>java CharToInt
Enter a char:
8
Char: 8
Integer: 8
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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: June 15, 2007

Related Tutorials

Discuss: Convert Character into Integer   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Pulak Ghosh
February 15, 2012
char 2 int in Java

class int2char { public static void main(String args[]) { int a = 99; char c = (char) a; System.out.println(c); } }
DMCA.com