0
import java.util.Scanner;

public class Ex4_9 {

 public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("enter character");

    String a = input.nextLine();
    char ch = a.charAt(0);
    if (a.length() == 1){
    System.out.println("The character entered is " + ch);
    System.out.println(" the Unicode for character " + ch + " " + ??);
    }
    else
    System.out.println("complain about the number of characters.");
 }

}

I want to be able to enter E and java display 69. what do i need to fill in for the ??

Zied R.
  • 4,964
  • 2
  • 36
  • 67
  • Unicode is strange in Java. There is a possibility that a single java character will have two codepoints. Here is an answer that kind of sort of answers what you're looking to do: http://stackoverflow.com/questions/2220366/get-unicode-value-of-a-character – hooknc Oct 07 '14 at 17:11
  • @hooknc No, there is not a possibility that a _Java_ character will have two code points... – bcsb1001 Oct 17 '14 at 19:58
  • @bcsb1001 you are correct. I had my terms crisscrossed. A codepoint can have two characters. – hooknc Oct 17 '14 at 20:27

2 Answers2

0

You want to use codePointAt...

System.out.println(" the Unicode for character " + a + " " + a.codePointAt(0));
Jason
  • 3,777
  • 14
  • 27
-1

Simply cast ch to int to get its Unicode value, as in

System.out.println(" the Unicode for character " + ch + " " + ((int) ch));

As for comments, this will work for any char, but not any Unicode code point. However, the question asks for a solution for a char, which mine works for. ch is initialised as a.charAt(0), which doesn't work for surrogates anyway, so I don't see any reason in the downvote.

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
  • @Jason Yes, I tested by printing `(int) 'E'` and it printed `69`. Also, this is something I have used for a long time. – bcsb1001 Oct 07 '14 at 17:52
  • 1
    I think it will only work for code points within the first plane. – Jason Oct 07 '14 at 18:25
  • @Jason Also, a Java `char` can only represent a character from the first plane... – bcsb1001 Oct 08 '14 at 18:58
  • I think you got downvoted because the solution doesn't work for all unicode values (it only handles code points in the first plane and it fails for anything that requires surrogate pairs). It generally works for the first plane though since java uses UTF-16, and UTF-16 char values (16 bits) correspond with their code points (below surrogate pair values). – Jason Oct 08 '14 at 21:04