Java Program to find ASCII values of String Characters

Write a Java Program to find ASCII values of String Characters with an example. In this java example, we used the String codePointAt function to return the character’s ASCII code.

import java.util.Scanner;

public class ASCIIValuesOfStringChars {
	private static Scanner sc;
	
	public static void main(String[] args) {
		String asciistr;
		int i = 0;
			
		sc= new Scanner(System.in);

		System.out.print("\n Please Enter any Sentence for ASCII Codes :  ");
		asciistr = sc.nextLine();
			
		while(i < asciistr.length())
		{
			System.out.println("The ASCII Value of " + asciistr.charAt(i) +
					" Character = " + asciistr.codePointAt(i));
			i++;
		}
	}
}

Java ASCII value of string characters output

 Please Enter any Sentence for ASCII Codes :  hello
The ASCII Value of h Character = 104
The ASCII Value of e Character = 101
The ASCII Value of l Character = 108
The ASCII Value of l Character = 108
The ASCII Value of o Character = 111

Java Program to find ASCII values of String Characters using For loop

import java.util.Scanner;

public class ASCIIValuesOfStringChars1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		String asciistr;
		int i;
			
		sc= new Scanner(System.in);

		System.out.print("\n Please Enter any Sentence for ASCII Codes :  ");
		asciistr = sc.nextLine();
			
		for(i = 0; i < asciistr.length(); i++)
		{
			System.out.println("The ASCII Value of " + asciistr.charAt(i) +
					" Character = " + asciistr.codePointAt(i));
		}
	}
}
Java Program to find ASCII values of String Characters 2

It is another Java example to find ASCII values of Characters in a string. Here, we are typecasting character to an integer, which will show you the ASCII value of that character.

import java.util.Scanner;

public class ASCIIValuesOfStringChars2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		String asciistr;
		int i;
			
		sc= new Scanner(System.in);

		System.out.print("\n Please Enter any Sentence for ASCII Codes :  ");
		asciistr = sc.nextLine();
			
		for(i = 0; i < asciistr.length(); i++)
		{
			char ch = asciistr.charAt(i);
			int num = (int) ch;
			System.out.println("The ASCII Value of " + ch +
					" Character = " + num);
		}
	}
}
 Please Enter any Sentence for ASCII Codes :  Java Coding
The ASCII Value of J Character = 74
The ASCII Value of a Character = 97
The ASCII Value of v Character = 118
The ASCII Value of a Character = 97
The ASCII Value of   Character = 32
The ASCII Value of C Character = 67
The ASCII Value of o Character = 111
The ASCII Value of d Character = 100
The ASCII Value of i Character = 105
The ASCII Value of n Character = 110
The ASCII Value of g Character = 103