Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Trying to display Unicode characters

  1. #1
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Trying to display Unicode characters

    Hello,

    I'm not sure where to post this. I'm trying to write a simple GUI Unicode Viewer. The user enters the Hex code in the JTextField and the program generates the Unicode character for that code as well as the next 320 Unicode characters ad displays them in a JTextArea. However, when I try to insert the "\u" escape character in the front of the Hex code I get an "illegal unicode escape" compiler error. How do I fix this? I tried converting the hex string to an int and explicitly cast it to a char which worked for small values but caused a NumberFormatException on larger values

    Here is the code:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
     
    public class Exercise31_1 extends JApplet {
      private JTextField jtfUnicode = new JTextField(5);
      private JTextArea jtaUnicode = new JTextArea();
      private String hexCode;
      private int hexNumber;
     
      public void init() {
        JPanel p1 = new JPanel(new BorderLayout());
    	p1.add(jtfUnicode, BorderLayout.CENTER);
    	p1.setBorder(new TitledBorder("Specify Unicode"));
    	JPanel p2 = new JPanel(new BorderLayout());
    	p2.add(new JScrollPane(jtaUnicode), BorderLayout.CENTER);
    	jtaUnicode.setEditable(false);
    	add(p1, BorderLayout.NORTH);
    	add(p2, BorderLayout.CENTER);
     
    	//jtaUnicode.append("\u4F20");
     
    	jtfUnicode.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent e) {
    	    hexCode = jtfUnicode.getText().trim();
    		hexNumber = HexToDecimalConversion.hexToDecimal(hexCode);
    	    for (int i = 0; i < 20; i++) {
    		  jtaUnicode.append(hexCode + " ");
    		  for (int j = 1; j < 16; j++) {
    		    hexNumber++;
    			hexCode = Decimal2HexConversion.decimalToHex(hexNumber);
    			jtaUnicode.append(hexCode+ ",");
    		  }
    		  jtaUnicode.append("\n\n");
    		  hexNumber++;
    		  hexCode = Decimal2HexConversion.decimalToHex(hexNumber);
    		}
     
    	  }
     
    	});
     
      }
     
      public static void main(String[] args) {
        Exercise31_1 applet = new Exercise31_1();
    	JFrame frame = new JFrame("Unicode viewer");
     
    	frame.add(applet, BorderLayout.CENTER);
    	applet.init();
    	applet.start();
     
    	frame.setSize(500, 300);
    	frame.setLocationRelativeTo(null);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);
     
      }
     
    }

    Thanks in advance for the help.

    Casey


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Trying to display Unicode characters

    The error I get is

    cannot find symbol - HexToDecimalConversion

    on this line:
    hexNumber = HexToDecimalConversion.hexToDecimal(hexCode);


    Is this the error you are trying to solve or is it just missing?

  3. #3
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Trying to display Unicode characters

    Quote Originally Posted by ChristopherLowe View Post
    The error I get is

    cannot find symbol - HexToDecimalConversion

    on this line:
    hexNumber = HexToDecimalConversion.hexToDecimal(hexCode);


    Is this the error you are trying to solve or is it just missing?
    Sorry, no. HexToDecimalConversion is another program I wrote and that works fine. I just realized that I took out the bad code. This is the line that was causing me trouble

    jtaUnicode.append("\u" + hexCode);

    The compiler error I get is "illegal unicode escape "

    The code I listed runs but, instead of the Unicode character being displayed, it displays the hex number. And when I try to insert the \u escape in front of the hex number I get the compiler error.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to display Unicode characters

    \u is a compiler feature allowing you to enter Unicode in your source program
    In your source there is no value after the \u so the compiler complains.

    What data type is hexCode? What does it contain? How do you want it converted to a String to display in the text area? Can you give an example?

  5. #5
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Trying to display Unicode characters

    Quote Originally Posted by Norm View Post
    \u is a compiler feature allowing you to enter Unicode in your source program
    In your source there is no value after the \u so the compiler complains.

    What data type is hexCode? What does it contain? How do you want it converted to a String to display in the text area? Can you give an example?
    hexCode is a String variable. The user enters a four digit hex code (ie 4F20) and the text area is supposed to display the Unicode character for that hexCode and the next 320 characters. But all it displays of course is the hexcode. But if I hard code "\u4F20" into the append method it displays the appropriate character (in this case a chinese character). I tried to combine the string hexcode with the string \u to get the desired result but it is obviously illegal to do that. The only reason I am am converting from hex to decimal and back is so that I can increment the hex number to display the subsequent characters.

    What i want to do is find a way to take the hex code the user enters and display the eqvialent Unicode character.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to display Unicode characters

    Given a String as input, you could use an Integer parse method to convert that to an int value which you could then increment easily and convert its new value to hex again by using another of the Integer class's methods.

    take the hex code the user enters and display the eqvialent Unicode character.
    Have you tried casting to char?

  7. #7
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Trying to display Unicode characters

    I tried that and it worked with smaller values like 0077 but I got a NumberFormatException on larger values like 4F20.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to display Unicode characters

    I got a NumberFormatException on larger values like 4F20.
    Can you post code that gets that error?

  9. #9
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Trying to display Unicode characters

    Quote Originally Posted by Norm View Post
    Can you post code that gets that error?
    Unfortunately, I'm not at home right now, so I'll have to post it on Monday. But, if memory serves me correctly, I used int hexNumber = Integer.parseInt(hexCode) to convert the hexCode string to an int and then used char hexChar = (char)hexNumber to cast it.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to display Unicode characters

    Check the parseInt method's API doc. It defaults to base 10. 4F20 is not base 10 so you will have to specify what base you what the parseInt method to use.

  11. The Following User Says Thank You to Norm For This Useful Post:

    kc120us (January 16th, 2012)

  12. #11
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Trying to display Unicode characters

    Quote Originally Posted by Norm View Post
    Check the parseInt method's API doc. It defaults to base 10. 4F20 is not base 10 so you will have to specify what base you what the parseInt method to use.
    That could definetely be the problem with the NumberFormatException error. I think I will try taking the string from the user input and converting it to decimal first with my conversion program (which returns an int) and cast the result to char. I will let you know if it works.

    Thanks.

  13. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Trying to display Unicode characters

    Or, just call the API function to do it for you.

  14. #13
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Trying to display Unicode characters

    Thanks to Norm and everyone else. Using Integer.parseInt(String s, int radix) worked. It allowed me to cast the int hexCode to a char

Similar Threads

  1. set database unicode
    By serdar in forum Java Theory & Questions
    Replies: 0
    Last Post: July 21st, 2011, 04:42 AM
  2. Strange problem printing Unicode characters
    By sophist42 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 29th, 2011, 10:53 AM
  3. combobox unicode problem
    By trexi in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: January 18th, 2011, 11:06 AM
  4. java and unicode ...
    By abesha in forum Java Theory & Questions
    Replies: 2
    Last Post: November 15th, 2010, 11:25 AM
  5. print unicode charecter
    By zulqar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 01:54 PM