The following code is to accept any character and print its ASCII Code.
Code:-
import java.util.*;
public class ASCIICode{
Scanner scan=new Scanner(System.in);
int asc;
char ch;
public void input(){
try{
System.out.println("Enter any character: ");
String string=scan.next();
ch=string.charAt(0);
}
catch(Exception e){
System.out.println("Error Code: "+e);
System.exit(0);
}
}
public void compute(){
asc=ch;
System.out.println("ASCII Code of \""+ch+"\" is: "+asc);
}
public static void main(String[] args){
ASCIICode obj=new ASCIICode();
obj.input();
obj.compute();
}
}
Output:
Enter any character:
A
ASCII Code of "A" is: 65
Code:-
import java.util.*;
public class ASCIICode{
Scanner scan=new Scanner(System.in);
int asc;
char ch;
public void input(){
try{
System.out.println("Enter any character: ");
String string=scan.next();
ch=string.charAt(0);
}
catch(Exception e){
System.out.println("Error Code: "+e);
System.exit(0);
}
}
public void compute(){
asc=ch;
System.out.println("ASCII Code of \""+ch+"\" is: "+asc);
}
public static void main(String[] args){
ASCIICode obj=new ASCIICode();
obj.input();
obj.compute();
}
}
Output:
Enter any character:
A
ASCII Code of "A" is: 65
Comments