How to Print ASCII Value of characters in Java
ASCII stands for American standard code for information interchange. It represents the numerical value of characters. It is a 7 bit character set contains 128 characters In this article we are going to see how to print the ASCII value of characters using Java program here, you will see the two methods by which you can print the ASCII value of characters-
- Assigning a variable into a int variable
- Using type cast
Assigning a variable into a int variable
public class AsciiValueCheck {
public static void main(String[] args)
{
char a = 'J'; //character whose value wwill find
int n = a; //integer variable takes the character value
System.out.println(n); //ASCII value of'J' will be printed
}
}
74
here, we have created a char type and assign their value into a int variable and simply output it so 74 is the ASCII value of 'J' similarly you can check the rest of the letters.
Defining the character value into the integer type directly -
public class AsciiValueCheck {
public static void main(String[] args)
{
int n = 'A';
System.out.println(n);
}
}
65
Using Type Cast
public class AsciiValueCheck {
public static void main(String[] args)
{
char a = 'A';
int n = (int) a;
System.out.println(n);
}
}
65
Taking character from User and prints accordingly-
Program 01 - "User is asked to enter a character value and corresponding ASCII value is printed ".
Problem statement - "Taking character input from the user repeatedly and prints their corresponding ASCII value accordingly"-
import java.util.Scanner;
public class AsciiValueCheck {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("Enter a character whose ASCII value you want to print");
char n = sc.next().charAt(0);
int asciiValue = (int) n;
System.out.println("the ascii value of"+n+"is:"+asciiValue);
}
}
}
Output:
Enter a character whose ASCII value you want to print
A
the ascii value ofAis:65
Enter a character whose ASCII value you want to print
a
the ascii value ofais:97
Enter a character whose ASCII value you want to print
H
the ascii value ofHis:72
Enter a character whose ASCII value you want to print
Jack
the ascii value ofJis:74 //ASCII value of 'J' remember
Enter a character whose ASCII value you want to print //running repeatedly
Printing ASCII value from A to Z
If you want to print the ASCII value of all the characters from 'A' to 'Z' or from 'a' to 'z' you can do this too, just by running a for loop as shown below-
public class AtoZAscii {
public static void main(String[] args)
{
for(int i=65;i<=90;i++)
{
System.out.println("the ASCII value of "+(char)i+"is"+i);
}
}
}
the ASCII value of Ais65
the ASCII value of Bis66
the ASCII value of Cis67
the ASCII value of Dis68
the ASCII value of Eis69
the ASCII value of Fis70
the ASCII value of Gis71
the ASCII value of His72
the ASCII value of Iis73
the ASCII value of Jis74
the ASCII value of Kis75
the ASCII value of Lis76
the ASCII value of Mis77
the ASCII value of Nis78
the ASCII value of Ois79
the ASCII value of Pis80
the ASCII value of Qis81
the ASCII value of Ris82
the ASCII value of Sis83
the ASCII value of Tis84
the ASCII value of Uis85
the ASCII value of Vis86
the ASCII value of Wis87
the ASCII value of Xis88
the ASCII value of Yis89
the ASCII value of Zis90
you can also do the same for letter from 'a' to 'z' running a loop from 'a' ASCII value to the 'z' ASCII value.
Hello, visitor your comments are so helpful for us so do not hesitate just write the comment we read all your comments so don't think your comment goes waste.