(Probably simple) problem with displaying Unicode characters

M

mfeher

Hi all,

I am getting back into Java programming after a long layoff and now
that I'm doing it everyday, I'm remembering the things I enjoyed about
it and the things that I find maddening. :) I'm trying to write a
simple program to both test my old skills as well as develop code for
use later, when I will need it on a later project.

Right now I'm simply trying to display a Unicode character in the
Eclipse output window. I have a full-up WinXP system with a ton of
fonts (more on that in a moment) but don't seem to see what is wrong
with my program.

I never did much with Unicode years before and never had it succeed for
me then, either, if I recall correctly. But I don't think the
process/requirements are that hard:

- Have Unicode fonts installed
- Use the correct encoding in the Java output stream
- Look up the correct character(s) in the Unicode tables

Here's the program as it stands now (one file called Main.java):


import java.io.*;

public class Main {

/**
* @param args
*/
public static void main(String[] args) throws
UnsupportedEncodingException {
String unicodeMessage = "\u03B2";

// Print out a Unicode character
System.out.println("We attempt to print out the Greek character
beta:");
PrintStream output = new PrintStream(System.out, true, "UTF-8");
output.println(unicodeMessage);
}
}

Here's the output I get:

We attempt to print out the Greek character beta:
β


Please, can someone spot the (probably simple) error in my
setup/coding? Thanks!

Mike
 
K

Knute Johnson

Hi all,

I am getting back into Java programming after a long layoff and now
that I'm doing it everyday, I'm remembering the things I enjoyed about
it and the things that I find maddening. :) I'm trying to write a
simple program to both test my old skills as well as develop code for
use later, when I will need it on a later project.

Right now I'm simply trying to display a Unicode character in the
Eclipse output window. I have a full-up WinXP system with a ton of
fonts (more on that in a moment) but don't seem to see what is wrong
with my program.

I never did much with Unicode years before and never had it succeed for
me then, either, if I recall correctly. But I don't think the
process/requirements are that hard:

- Have Unicode fonts installed
- Use the correct encoding in the Java output stream
- Look up the correct character(s) in the Unicode tables

Here's the program as it stands now (one file called Main.java):


import java.io.*;

public class Main {

/**
* @param args
*/
public static void main(String[] args) throws
UnsupportedEncodingException {
String unicodeMessage = "\u03B2";

// Print out a Unicode character
System.out.println("We attempt to print out the Greek character
beta:");
PrintStream output = new PrintStream(System.out, true, "UTF-8");
output.println(unicodeMessage);
}
}

Here's the output I get:

We attempt to print out the Greek character beta:
β


Please, can someone spot the (probably simple) error in my
setup/coding? Thanks!

Mike

Mike:

The default character set is UTF-16 which will display your 'beta'
character just fine. It won't however display on the console, at least
I know it won't on a Winblows XP system.

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Main {

/**
* @param args
*/
public static void main(String[] args) throws
UnsupportedEncodingException {
Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
f.add(new Label("\u03b2"));
f.pack();
f.setVisible(true);
}
}
 
R

Roedy Green

The default character set is UTF-16 which will display your 'beta'
character just fine. It won't however display on the console, at least
I know it won't on a Winblows XP system.

Isn't that weird that the console remained stuck at the stage of
evolution of DOS. On my machine, it even uses the old IBM OEM char
set.

It needs an update: colour, traceback to the source line that created
it, Tee to a file, scrollback, permanent log record, encodings.
 
Z

Zerex71

Hi Knute,

Thanks for responding. The funny thing is, I think anyone reading this
post must think I've gone mad, because looking at my code now, the beta
displays just fine. It did not, however, when I tried posting - it
came up looking like a "careted" capital I to the power of 2.

My question then becomes:
1. Is my code at all buggy? (I maintain that it's not and that it's
perfectly legal; maybe I'm using the wrong encoding? I got it from an
online example).
2. Do I have a possible font problem?

I forgot to mention perhaps that the output is being sent to the
Eclipse debug/output console, and that's where it's not displaying
properly.

Mike
 
O

Oliver Wong

Zerex71 said:
Hi Knute,

Thanks for responding. The funny thing is, I think anyone reading this
post must think I've gone mad, because looking at my code now, the beta
displays just fine. It did not, however, when I tried posting - it
came up looking like a "careted" capital I to the power of 2.

My question then becomes:
1. Is my code at all buggy? (I maintain that it's not and that it's
perfectly legal; maybe I'm using the wrong encoding? I got it from an
online example).
2. Do I have a possible font problem?

I forgot to mention perhaps that the output is being sent to the
Eclipse debug/output console, and that's where it's not displaying
properly.

I've never seen anyone setting the encoding on a printstream directed to
the console before. Presumably, the OS (or Eclipse in this case), will set
up the encoding the way it likes, so setting it differently might mess
things up.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top