The following Code is to get the ASCII Code (American Standard Code for Information Interchange) of any String entered by the user.
Code:-
import java.util.*;
public class StringASCII{
Scanner scan=new Scanner(System.in);
String string;
int ascii;
char asc;
public void input(){
try{
System.out.println("Enter any String: ");
string=scan.nextLine();
}
catch(Exception e){
System.out.println("Error Code: "+e);
System.exit(0);
}
}
public void compute(){
for(int i=0; i<string.length(); i++){
asc=string.charAt(i);
ascii=(int)asc;
System.out.println("ASCII Code of \""+asc+"\" is: "+ascii);
}
}
public static void main(String[] args){
StringASCII ascii=new StringASCII();
ascii.input();
ascii.compute();
}
}
Output is Like:
Enter any String:
ASCII CODE
ASCII Code of "A" is: 65
ASCII Code of "S" is: 83
ASCII Code of "C" is: 67
ASCII Code of "I" is: 73
ASCII Code of "I" is: 73
ASCII Code of " " is: 32
ASCII Code of "C" is: 67
ASCII Code of "O" is: 79
ASCII Code of "D" is: 68
ASCII Code of "E" is: 69
Code:-
import java.util.*;
public class StringASCII{
Scanner scan=new Scanner(System.in);
String string;
int ascii;
char asc;
public void input(){
try{
System.out.println("Enter any String: ");
string=scan.nextLine();
}
catch(Exception e){
System.out.println("Error Code: "+e);
System.exit(0);
}
}
public void compute(){
for(int i=0; i<string.length(); i++){
asc=string.charAt(i);
ascii=(int)asc;
System.out.println("ASCII Code of \""+asc+"\" is: "+ascii);
}
}
public static void main(String[] args){
StringASCII ascii=new StringASCII();
ascii.input();
ascii.compute();
}
}
Output is Like:
Enter any String:
ASCII CODE
ASCII Code of "A" is: 65
ASCII Code of "S" is: 83
ASCII Code of "C" is: 67
ASCII Code of "I" is: 73
ASCII Code of "I" is: 73
ASCII Code of " " is: 32
ASCII Code of "C" is: 67
ASCII Code of "O" is: 79
ASCII Code of "D" is: 68
ASCII Code of "E" is: 69
Comments