Solved

Java find the ascii value

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now