Beginner's question

  • Thread starter =?iso-8859-1?B?Sm9yZ2UgQ2Fs4XM=?=
  • Start date
?

=?iso-8859-1?B?Sm9yZ2UgQ2Fs4XM=?=

Hi,

I'm new to java and I'm making some little things with it, but with
some test I get weird results. Look this example:

package variables;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println((char)164);
System.out.println((char)241);
System.out.println((int)'¤');
System.out.println((int)'ñ');

}

}

When I run this code in Eclipse (Shift+Alt+X, J) I get this (wrong)
results:

¤
ñ
164
241

Then I copied to UEdit32, save it as Test.java, compiled using javac
Test.java and run with java Test and get this (right) results:

C:\tools>java Test
ñ
±
164
241

C:\tools>

My system is Windows XP sp 2 spanish version with java version
"1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)


Eclipse SDK

Version: 3.2.0
Build id: M20060629-1905

MyEclipse Web Services Support

Version: 5.0.1
Build id: 20060810-5.0.1-GA

I'm trying to make a module for an existing application and I'm afraid
this can happen on production and things get messy.

What do you think can be causing this? How can I solve it?

Thanks in advance,

Jorge
 
O

Oliver Wong

Jorge Calás said:
System.out.println((char)164);
System.out.println((char)241);
System.out.println((int)'¤');
System.out.println((int)'ñ'); [...]
When I run this code in Eclipse (Shift+Alt+X, J) I get this (wrong)
results:

¤
ñ
164
241

Then I copied to UEdit32, save it as Test.java, compiled using javac
Test.java and run with java Test and get this (right) results:

C:\tools>java Test
ñ
±
164
241

Probably your two editors (Eclipse and UEdit32) are using different
encodings. You can avoid encoding issues alltogether by escaping like so:

System.out.println('\u00f1'); /*Prints out unicode character 00F1, Latin
Small Letter N with Tilde.*/

You can find a chart of all unicode characters at
http://www.unicode.org/charts/

- Oliver
 
?

=?iso-8859-1?B?Sm9yZ2UgQ2Fs4XM=?=

Thanks for your response Oliver. If read it the right way, user input
will not be affected by this issue, regardless the editor I use to
code. Right?

Thanks!

Jorge.


Oliver said:
Jorge Calás said:
System.out.println((char)164);
System.out.println((char)241);
System.out.println((int)'¤');
System.out.println((int)'ñ'); [...]
When I run this code in Eclipse (Shift+Alt+X, J) I get this (wrong)
results:

¤
ñ
164
241

Then I copied to UEdit32, save it as Test.java, compiled using javac
Test.java and run with java Test and get this (right) results:

C:\tools>java Test
ñ
±
164
241

Probably your two editors (Eclipse and UEdit32) are using different
encodings. You can avoid encoding issues alltogether by escaping like so:

System.out.println('\u00f1'); /*Prints out unicode character 00F1, Latin
Small Letter N with Tilde.*/

You can find a chart of all unicode characters at
http://www.unicode.org/charts/

- Oliver
 
O

Oliver Wong

Jorge Calás said:
Thanks for your response Oliver. If read it the right way, user input
will not be affected by this issue, regardless the editor I use to
code. Right?

If you write a GUI app using like Swing, then Swing will take care of
all the details such that internally, your program will receive the correct
character sequences.

For console applications, you may or may not have issues.

For file input/ouput, you need to specify encodings explicitly.

- Oliver
 
?

=?iso-8859-1?B?Sm9yZ2UgQ2Fs4XM=?=

Thanks again Oliver. ..

what about web apps?


Oliver said:
If you write a GUI app using like Swing, then Swing will take care of
all the details such that internally, your program will receive the correct
character sequences.

For console applications, you may or may not have issues.

For file input/ouput, you need to specify encodings explicitly.

- Oliver
 
O

Oliver Wong

[context is encoding]

Jorge Calás said:
Thanks again Oliver. ..

what about web apps?

In web-apps, what you receive is what the browser sends you, and it's
possible that the browser is poorly programmed, and might send any weird
random nonsense.

- Oliver
 
O

olle.sundblad

I have had a problem with this and if I remember correctly there are
some solutions:

- most browsers send form info in the vied page encoding (so specify
the html encoding on all pages).
- you can specify the encoding in a html form with the encoding
attribute (FireFox supported this but IE6 did not so it is not
recommended to only rely on this)
- you could put some non standard characters in a hidden field of the
form and from that field get the encoding by testing on the server.
- you can hope that most of your users use the same encoding and just
use that (and tell other users to set their browser to use that
encoding if they want to use it)

If someone has another solution I would like to hear it?
 
O

Oliver Wong

I have had a problem with this and if I remember correctly there are
some solutions:

- most browsers send form info in the vied page encoding (so specify
the html encoding on all pages).
- you can specify the encoding in a html form with the encoding
attribute (FireFox supported this but IE6 did not so it is not
recommended to only rely on this)
- you could put some non standard characters in a hidden field of the
form and from that field get the encoding by testing on the server.
- you can hope that most of your users use the same encoding and just
use that (and tell other users to set their browser to use that
encoding if they want to use it)

If someone has another solution I would like to hear it?

Thanks for sharing. The "hidden field" trick is really clever and I
hadn't thought of it.

- Oliver
 
?

=?iso-8859-1?B?Sm9yZ2UgQ2Fs4XM=?=

Thanks Olle and Oliver, I will keep doing some tests on it and I'll
comment later my results.

Jorge
 

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

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top