comptitbits
asked on
Java find the ascii value
Dear Experts,
I need your advice.
I want to enter any character (digits, letters and +)(*&^%$ etc) using the key board
and find the equivalent ASCII value of the character in Java.
Please let me know if there is any function or a easy way to do this.
500 points for the best answer
Thank you very much
I need your advice.
I want to enter any character (digits, letters and +)(*&^%$ etc) using the key board
and find the equivalent ASCII value of the character in Java.
Please let me know if there is any function or a easy way to do this.
500 points for the best answer
Thank you very much
ASKER
Dear mbodewes:
Thanks for your prompt reply
Is there any other way like:
Print "Enter a letter"
String letter = input.next();
(find the value of letter in ascii)
Print the ascii value
Thank you very much
Thanks for your prompt reply
Is there any other way like:
Print "Enter a letter"
String letter = input.next();
(find the value of letter in ascii)
Print the ascii value
Thank you very much
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Two variants:
1. Using the console (System.in)
System.out.println("Enter a character:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.i n));
int characterValue = br.read();
System.out.println(charact erValue);
2. Using a little window to prompt the user:
String input = JOptionPane.showInputDialo g("Enter a character:", "");
byte[] characterBytes = input.getBytes();
erBytes[0] );
1. Using the console (System.in)
System.out.println("Enter a character:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.i
int characterValue = br.read();
System.out.println(charact
2. Using a little window to prompt the user:
String input = JOptionPane.showInputDialo
byte[] characterBytes = input.getBytes();
// print first character (the following characters are ignored
System.out.println(charactASKER
Thanks mbodewes. It works. But, I want to find more easier way to do this.
My aim is make the code as simple as possible by using all kinds of materials out there. I am doing this just for fun. Thanks a lot you got 500 points
My aim is make the code as simple as possible by using all kinds of materials out there. I am doing this just for fun. Thanks a lot you got 500 points
BufferedReader br = new BufferedReader(new InputStreamReader(System.i
charStr = br.readLine();
Now you take the first character from the String using the String.charAt() method. You might want to check if the line just contains a single character.
Now you could just display the value of the char. Char is Unicode, but the first 128 characters are equal to the ASCII code.
You could also use charStr.getBytes("ASCII"),