In Java, to print ASCII (American Standard Code for Information Interchange) character with values we can use looping concepts such as while loop or for loop and we can also achieve ASCII character with value without using any methods or loops. Java internally converts the character value to an ASCII value.
ASCII value is a 8-bit character set contains 256 (ASCII control characters 0 to 31 and 127,they are unprintable), ASCII printable characters 32-126 (32 is a non-printing spacing character) and Extended ASCII characters 128-255).
However, 7 bits are sufficient to represent a character in ASCII with values start from 0 to 127. In this article we will learn through an example, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop concepts
Problem Statement
We have to write a java program to print ASCII (American Standard Code for Information Interchange) character with values. The program should take a character as input and print the ASCII value of the character. Also program should print all the ASCII values of all characters. These two actions will be done in our program. Let’s see an example.
Example:
For a character ( , Output should be 40.
Input character is: (
Ascii value of ( is: ( : 40
Logic to print ASCII character with values
There are multiple logics to print ASCII character with values in java programming language.
Logic 1: Assign char variables to int variables
We can print ASCII values(0 to 127) without looping statements.
- Take a character from the user using next().charAt(0) method.
- Converts the character ch to its ASCII value by implicitly assigning ch to an integer variable asciiValue. In Java, characters are represented internally as numeric ASCII values.
- Now, print the original character entered by the user and its corresponding ASCII value using print statement in Java.
Logic 2: Using loop concepts
Here we are using for loop but we can also use while loop.
- Take the integer number (Ascii value) from the user and assign it to the variable var.
- It uses a for loop to iterate starting from the user-defined integer var up to 255. This loop is controlled by for(int i= var; i<=255; i++).
- Inside the loop, it converts the integer i to its corresponding ASCII character using (char)i.
- Prints the ASCII value and its corresponding character using print statement.
- System.out.println(“ASCII value of character: ” + (char)i +”=”+i);
- This statement typically formats the output to display the ASCII character and its numeric value.
Java program to print ASCII character with values
Program 1: Assign char variables to int variables
In the below program we are assigning the char variables to integer variables to print ASCII values of characters in Java.
import java.util.Scanner;
public class AsciiValue {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter a character:");
char ch = s.next().charAt(0);
int asciiValue = ch; // Getting ASCII value of character
System.out.println("Ascii value of " + ch + " is: " + asciiValue);
s.close(); // Closing Scanner object
}
}
Output:
Please enter a character:
e
Ascii value of e is: 101
Java program explanation to print ASCII character with values
- The program starts by importing the
Scanner
class fromjava.util
to handle user input. - It asks the user to enter a character using
System.out.println("Please enter a character:");
. - The program reads the first character of the input using
s.next().charAt(0)
and assigns it to the variablech
. - To find the ASCII value of the character, it assigns
ch
to an integer variableasciiValue
(int asciiValue = ch;
). - The program then prints the original character and its ASCII value using
System.out.println("Ascii value of " + ch + " is: " + asciiValue);
. - For proper resource management, it closes the
Scanner
objects
withs.close()
after processing the input.
Program 2: Using for loop
In the below program we are using for loop to print the ASCII values of a characters in Java.
import java.util.Scanner;
public class AsciiValues {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please initialize the variable");
int var=s.nextInt();
for(int i=var;i<=255;i++) {
System.out.println("ASCII value of character: " + (char)i +"="+i);
}
s.close();
}
}
Output:
Please initialize the variable
253
ASCII value of character: ý=253
ASCII value of character: þ=254
ASCII value of character: ÿ=255
Java program explanation to print ASCII character with values
- The java program imports the
Scanner
class for user input. - In the
main
method, it asks the user to initialize a variable and reads the input. - It uses a
for
loop to iterate from the initialized variable value up to 255. - Inside the loop, it converts the integer value to its corresponding ASCII character using
(char)i
. - It then prints the ASCII value and character for each iteration.
- Finally, it closes the
Scanner
object to release resources.
Conclusion
In this article, we have learnt how print ASCII character with values in two ways in Java. First we have seen the logic for converting the characters into ASCII values internally by Java. Then we also seen the printing characters for a certain range of ASCII values and input value will be decided by the user. Then we implemented both programs.
Hope this article helped you in understanding the logics and programs clearly.
You can also check our another awesome tutorials:
- Java program to print all lowercase alphabets
- Print all uppercase alphabets in Java
- Calculate the percentage and grade based on marks of 5 subjects in Java
- Find all even numbers in a given range
Happy Java Programming!!