Solved

Java find the ascii value

Posted on 2008-10-11
5
3,376 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 500 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

719 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