Solved

Foreign characters

Posted on 2004-09-09
26
393 Views
Last Modified: 2013-12-03
I can type Russian, Hebrew etc into a JTextField and JTextArea and they display ok, but sending the strings over a socket seems to destroy them and they become question marks. What readers / writers / streams are best to use in this case?
0
Comment
Question by:afterburner
  • 12
  • 7
  • 4
  • +1
26 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12014192
use a Writer and check the problem isn't with whats reading the data at the other end.
0
 

Author Comment

by:afterburner
ID: 12014371
At the other end is always a buffered reader that reads the string. Or would the problem be in something else?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 80 total points
ID: 12014404
check the encoding being used
and is the oher end using a font that supports the characters being sent?
0
 

Author Comment

by:afterburner
ID: 12014430
I tried sending the string as a string captured from the JTextField (JTextField.getTExt();), then tried it as a new String using UTF, UTF-8, UTF-16, UTF-16BE, UTF-16LE, etc., and none of these worked, although with 16BE I think it was, the displayed return string looked a bit more promising, as it was not simply question marks, but I guess this is neither here nor there.

As I am using loopback, yes, the 'other end' is also using a font that supports the charset.
0
 

Accepted Solution

by:
Al-Khwarizmi earned 320 total points
ID: 12014612
When you instance your BufferedReader, be sure to pass it a Reader object configured with the encoding you're used.

As in

myReader = new BufferedReader ( new InputStreamReader ( myInputStream , "UTF-16" ) );
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12014655
>>and they become question marks

Where?
0
 

Author Comment

by:afterburner
ID: 12014807
>> be sure to pass it a Reader object configured  ...

that sounds like a good idea - I will try that.

>> Where?  ...

in the displaying JTextArea which receives the returned string.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12014839
>>in the displaying JTextArea which receives the returned string.

That's OK then

>>that sounds like a good idea - I will try that.

Let us know if it doesn't work. Use UTF-8 unless you have a specific reason not to
0
 

Author Comment

by:afterburner
ID: 12015006
>> Use UTF-8 unless you have a specific reason not to ...

Does the printerwriter *and* the reader have to be configured with the encoding?
0
 

Expert Comment

by:Al-Khwarizmi
ID: 12015122
If you use a PrintWriter too, the situation is similar:

PrintWriter myPrintWriter = new PrintWriter ( new OutputStreamWriter ( myOutputStream , "UTF-16" ) );

would be a good match for the reader above, if the input and output streams are connected as is the case with a socket.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 12015134
>>Does the printerwriter *and* the reader have to be configured with the encoding?

Readers/Writers have to match encoding-wise
0
 

Author Comment

by:afterburner
ID: 12015308
OK, it will take me a little while to change things. Two small points meanwhile  - why do you mention UTF-8 and AL-Khwarizmi UTF-16 ? ; and secondly, will the encoding still allow the transport of ASCII chars as is the case with the existing Printwriter and Reader that I'm using?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12015336
>>why do you mention UTF-8

It's the most economical way of transmitting Unicode

>>will the encoding still allow the transport of ASCII chars as is the case with the existing Printwriter and Reader that I'm using?

Yes - 'ascii' is a subset of Unicode
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:afterburner
ID: 12015961
That's it. Thanks 4 ur help(s).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12015979
8-)
0
 

Expert Comment

by:Al-Khwarizmi
ID: 12015985
I only mentioned UTF-16 as an example, sorry if that confused you. UTF-8, as CEHJ says, is probably a better option in your case. UTF-16 is more suited for heavy use of Asian languages, where it can save space.

Although I don't think you need it, if you want to learn more about unicode formats you can check http://www-106.ibm.com/developerworks/library/utfencodingforms/index.html?dwzone=unicode
0
 

Author Comment

by:afterburner
ID: 12016048
>> UTF-16 is more suited for heavy use of Asian languages ...

in fact, that is exactly where it comes in - but at least I know now, and appreciate your help v. much.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12019056
>>
UTF-8, as CEHJ says, is probably a better option in your case. UTF-16 is more suited for heavy use of Asian languages, where it can save space.
>>

Yes you're right. That comment of mine could have been quite misleading. I think it's better to say UTF-8 is more economical when there are mixed 'ascii' and higher characters
0
 

Author Comment

by:afterburner
ID: 12019434
Tell me if I need to open another question for this, but I have been trying it now with Japanese and Korean, and all I get are little squares in the JTextArea and JTextFields. Would any of you have anything to say on that? It's these languages - plus Chinese  - that I need in particular.
0
 
LVL 92

Expert Comment

by:objects
ID: 12022184
> and all I get are little squares in the JTextArea and JTextFields.

Are you using a font that supports the characters being used?
0
 

Author Comment

by:afterburner
ID: 12022271
>> Are you using a font that supports the characters being used?


I am, because I can type Japanese into Word - although I admit I am not sure of the role of IME in this, and fear it may be some kind of a 'closed interface', just for M$ purposes, which Java can't share.

0
 
LVL 92

Expert Comment

by:objects
ID: 12022334
Is the same process involved transferring the string, and if so what encoding are you using.
0
 

Author Comment

by:afterburner
ID: 12022430
Yes, it is the same process exactly. I tried with UTF-8 first and it didnt work so I tried UTF-16LE for no good reason :) which of course didnt work either.
0
 

Author Comment

by:afterburner
ID: 12022448
I think (but I am not sure) that I might have seen another choice apart from IME when installing the Asian support; if that's the case should I unistall IME and try another one?
0
 

Author Comment

by:afterburner
ID: 12022467
Ah, sorry, my comment about using a Font that supports Jap may not be right exactly, since I might of course not be using the same one in Word as I am in the Java app JTextField. Should I check that or is it a red herring?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12028699
You need to do something like:

textField.setFont(new Font("Batang", Font.PLAIN, 12));
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 55
bitbucket vs gitbucket 3 57
ArrayIndexOutOfBoundException 9 67
Apps blocked by Java 9 60
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

911 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

26 Experts available now in Live!

Get 1:1 Help Now