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.

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.

Convert Character into Integer

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