?
Solved

Java find the ascii value

Posted on 2008-10-11
5
Medium Priority
?
3,381 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:comptitbits
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:mbodewes
ID: 22695651
Well, first you need something to read out the string or character from the inputstream:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
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"), this will give you a byte[] of ASCII character, or an exception if the character is not an ASCII character. Then you display the value of the first byte (even though the value should always be 0..127, it might be a good idea to do charValue & 0xFF the value so you won't get any negative values (e.g. if you switch to a character set that runs from 0..255.
0
 

Author Comment

by:comptitbits
ID: 22695689
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
0
 
LVL 9

Accepted Solution

by:
mbodewes earned 2000 total points
ID: 22695719
The problem is that most consoles expect one line at the time. So even if you could read less than that in your Java application, you would still have the problem that the input may only be supplied the moment the user hits enter.

I cannot give you the Java code since this is way to likely to be an assignment. Could you please give it a try and come back when you're stuck?
0
 
LVL 2

Expert Comment

by:Monky42
ID: 22695735
Two variants:
1. Using the console (System.in)
        System.out.println("Enter a character:");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int characterValue = br.read();
        System.out.println(characterValue);

2. Using a little window to prompt the user:
        String input = JOptionPane.showInputDialog("Enter a character:", "");
        byte[] characterBytes = input.getBytes();
// print first character (the following characters are ignored
       System.out.println(characterBytes[0]);
0
 

Author Closing Comment

by:comptitbits
ID: 31505366
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
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question