Java Program to Display ASCII value

In this post, we will develop a program to display the ASCII value of alphabets in java. Based on this program we will also develop a program to display ASCII value of a to z in java.

ASCII stands for American Standard Code for Information Interchange. It was developed by the ANSI (American National Standards Institute) and it is used to interchange the information from a high-level language to low-level language. Machine or Computer understand only binary languages. So, the character data type represents integers.

Procedure to develop the program to display the ASCII value of alphabets in Java:-

1) Take an alphabet as input from the end-user
The next().charAt(0) is used to read character input. The next() method is used to read a word_upto space (String), and when we use the charAt(index) method of String class then it gives the character values of String at index position, so next().charAt(0) gives the char value at index 0 in the String.

2) We can get the ASCII value of char by converting it to the Integer data type (byte/short/int). Different ways for converting are,

char ch= 'a';

//direct assigning
int n1 = ch; 

// type-casting
int n2 = (int)ch;

The n1 and n2 variables hold the ASCII value of the character ‘a’. The below-given program demonstrates it.

Program to display ASCII value of alphabets in Java

import java.util.Scanner;

public class AlphabetASCIIValue {

   public static void main(String[] args) {

      // declare a char variable
      char ch ;
      // declare a int variable
      int value ;

      // create Scanner class object
      // to read input
      Scanner scan = new Scanner(System.in);

      // read alphabet
      System.out.print("Enter an alphabet:: ");
      ch = scan.next().charAt(0);

      // convert char to ascii value
      value = ch;

      // display ascii value
      System.out.println("ASCII value of "+
                     ch+" = "+value);

      // Or, 
      // we can directly display ASCII value
      // using type-casting (int)ch
      System.out.println("ASCII value of "+
                 ch+" = "+ (int)ch );

      // close Scanner class object
      scan.close();
     }
 }

The output for different test-cases are:-

Enter an alphabet:: a
ASCII value of a = 97
ASCII value of a = 97

Enter an alphabet:: 1
ASCII value of 1 = 49
ASCII value of 1 = 49

Enter an alphabet:: A
ASCII value of A = 65
ASCII value of A = 65

From the above program can find the ASCII value of any valid alphabets. The ASCII value of A to Z is 65 to 90, and the ASCII value of a to z in Java is 97 to 122. Similarly, the ASCII value of 0 to 9 is 48 to 57.

Display ASCII value in the range

We can also display ASCII value in range. For example:- ASCII value of a to z in java. For this purpose, we need to take the help of a loop. You can use while loop, for loop, or do-while.

import java.util.Scanner;

public class ASCIIValueInRange {

   public static void main(String[] args) {

      // declare variables
      char minRange , maxRange;

      // create Scanner class object
      Scanner scan = new Scanner(System.in);

      // read inputs
      System.out.print("Enter minRange value:: ");
      minRange = scan.next().charAt(0);
      System.out.print("Enter maxRange value:: ");
      maxRange = scan.next().charAt(0);

      // display ASCII values
      System.out.println("ASCII value of "+
                 minRange+" to "+maxRange+" :: ");

      for(char i = minRange; i<=maxRange; i++) {
         System.out.print(i+"="+(int)i);
         System.out.print(",\t"); //give space
      }

      // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases:-

Enter minRange value:: a
Enter maxRange value:: z
ASCII value of a to z ::
a=97, b=98, c=99, d=100, e=101, f=102, g=103, h=104, i=105, j=106, k=107, l=108, m=109, n=110, o=111, p=112, q=113, r=114, s=115, t=116, u=117, v=118, w=119, x=120, y=121, z=122,

Enter minRange value:: A
Enter maxRange value:: Z
ASCII value of A to Z ::
A=65, B=66, C=67, D=68, E=69, F=70, G=71, H=72, I=73, J=74, K=75, L=76, M=77, N=78, O=79, P=80, Q=81, R=82, S=83, T=84, U=85, V=86, W=87, X=88, Y=89, Z=90,

Enter minRange value:: 0
Enter maxRange value:: 9
ASCII value of 0 to 9 ::
0=48, 1=49, 2=50, 3=51, 4=52, 5=53, 6=54, 7=55, 8=56, 9=57,

ASCII values of A to Z

A 65
B 66
C 67
Z 90

ASCII values of a to z

a 97
b 98
c 99
z 122

From these tables you can observe that there is constant difference of 32 in between the lowercase character and uppercase character.

(A = 65) + 32 = (a = 97)
(B = 66) + 32 = (b = 98)
(C = 67) + 32 = (c = 99)
……………………………………
……………………………………
(Z = 90) + 32 = (z = 122)

ASCII values of 0 to 9

0 47
1 48
2 49
9 57

In the previous program we displayed ASCII value of an alphabet but we can also reverse it. In the below program we will develop a Java program to print alphabets from ASCII values.

import java.util.Scanner;

public class AlphabetFromASCIIValue {

   public static void main(String[] args) {

      Scanner scan = new Scanner(System.in);
      System.out.print("Enter ASCII value:: ");
      int ascii = scan.nextInt();

      // display alphabet value
      System.out.println("Alphabet for ASCII value "+ 
                     ascii+" is = "+(char)ascii);

      // close Scanner class object
      scan.close();

     }
 }

Output for different test-cases:-

Enter ASCII value:: 100
Alphabet for ASCII value 100 is = d

Enter ASCII value:: 65
Alphabet for ASCII value 65 is = A

Enter ASCII value:: 65535
Alphabet for ASCII value 65535 is = ?

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *