reading DBCS data (japanese) from system.in

N

Nirmal R

Hi all,

I am trying to write one java client/server application in Solaris.

I have problem in Client side.

Actually, the client will read the user data using "System.in" and send
the data to user.
It's working fine for user name in english.

The problem comes when I try to read Japanese characters.
the code used to read data is below,

batchReader = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("User : ");
cl = batchReader.readLine();
System.out.println("Line >> " + cl);



for(i = 0; i < cl.length(); i++) {
System.out.print((int) cl.charAt(i));
System.out.print(", ");
}
System.out.println("");
batchReader.close();



for "export LANG=en_US.UTF-8", it reads the data properly,
but for "export LANG=" it prints some junk value.

rnirmal@snds1$ export LANG=en_US.UTF-8
rnirmal@snds1$ java nread
User : ã†ã‡ãŒ.company
Line >> ã†ã‡ãŒ.company
12358, 12359, 12364, 46, 99, 111, 109, 112, 97, 110, 121,

rnirmal@snds1$ export LANG=
rnirmal@snds1$ java nread
User : ã†ã‡ãŒ.company
Line >> ?????????.company
65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 46, 99,
111, 109, 112, 97, 110, 121,
rnirmal@snds1$

I need a common way to read data independent of encoding or "LANG"
value.
Please help me to fix the issue,

really appriciate your help.

Thanks,
Nirmal R.
 
J

John O'Conner

Nirmal said:
The problem comes when I try to read Japanese characters.
the code used to read data is below,

batchReader = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("User : ");
cl = batchReader.readLine();
System.out.println("Line >> " + cl);



for(i = 0; i < cl.length(); i++) {
System.out.print((int) cl.charAt(i));
System.out.print(", ");
}
System.out.println("");
batchReader.close();

I need a common way to read data independent of encoding or "LANG"
value.

You need to stop depending on the "default" encoding of your host
system. You are closely tying your input and output results to your
console encoding, which you are controlling by the LANG environment
variable.

I suggest two things:
1. Get user input from a Swing GUI. The input is guaranteed to be
Unicode characters. Display the input with a Swing GUI...the output is
Unicode rendered properly in a Swing component.
2. If you insist on reading from the console and you know that the input
is UTF-8 regardless of what the LANG setting is, then create your
InputStreamReader appropriately with a "UTF-8" charset parameter.

Regards,
John O'Conner
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top