Solved

Java find the ascii value

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 39
maven module vs maven project 3 74
login form jsp example 2 53
Selenium docs api java index 3 68
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

726 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