Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

output the £ sign

Status
Not open for further replies.

asm1

Technical User
Oct 9, 2003
77
GB
How do i output the British pound sign to the standard output. when i try i get a u with a mark ontop of it. does any one know why this is? thanks
 
I to have exactly the same problem the only way i have found around it is to do the following...

import java.io.*; //Need this..

public class poundSign{
public static void main(String[] arg){
try {
OutputStreamWriter osw = new OutputStreamWriter
(System.out, "Cp850");
PrintWriter out = new PrintWriter (osw);

System.out.println("£"); //prints out a funny sign

out.print ("£"); //£ is displayed
out.flush ();
}
catch (Exception e) {
System.out.println (" pounds");
}
}
}

Hope this helps you. There may be another way of doing it but this is the only way i have found so far.
 
thanks for sharing that. If any body knows of another way or why this is would you please let me know.
 
Caveat: I am assuming that you are using a Windows/DOS system, so YMMV.

From the PrintStream javadoc: All characters printed by a PrintStream are converted into bytes using the platform's default character encoding. (Emphasis mine)

By default, all DOS consoles use MS-DOS Codepage 437 (US) as the default character encoding.

If you change this to Microsoft Windows Codepage 1252 (ANSI) using the "chcp" command (e.g., "c:\> chcp 1252"), you'll get the desired output from:

public class PoundSign{
public static void main(String[] pArgs) {
char thisChar = 0xA3;
System.out.println(thisChar);
}
}

HTH.


Chris Ciulla
 
thanks thats much clearer, i will give that a try
 
Try this assignment:

class PoundSign
{
public static void main(String [] args)
{
final char POUNDSIGN = 339; // set a constant to hold a pound character

System.out.println(POUNDSIGN + "50\n\n\n"); // does it work? Try it here.
}
}


// arthurp
 
thanks that worked and was simple.
 
Here are two more options:

Writer w = new OutputStreamWriter( System.out, "Cp437" );
PrintWriter out = new PrintWriter (w);
out.println("Pound sign: \u00a3");
out.flush();


Alternatively you can define the encoding on the java command line.

java -Dfile.encoding=cp437 MyJavaProgram

MyProgram.java
...
System.out.println("Pound sign: \u00a3");
...

Sean McEligot
 
I apologise for not answering before now, I have had some problems but no time to sort them. Anyhow thx for all your replies.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top