I'm trying to implement a training task and output chess pieces to the console, but no matter how I change the encoding, the console still shows question marks instead of a chess piece. I will be grateful for your help! i'm using Intelij Idea
public class Chess {
public static void main(String[] args) {
System.out.println("♝");
}
}
and in console i see : ? insread of ♝
Im trying UTF - 8, and many others Encodings, but nothig does work. I'm om 8.1 windows
Related
I'm learning to code Java...
I just installed the Java runtime environment and Visual Studio Code, and wrote this:
public class IterationDemo
{
public static void main(String[] args)
{
System.out.println('x');
}
}
However, I don't see any output, both under output or terminal. Only kind of a rectangle (▯) under terminal.
I've tried to find a solution googling, stack and so on but no answer yet...
what can I do?
Thanks!
OK; got solved on its own - I ended up clearing the cache and then the output came :)
Thanks myself!
I'm new to java so excuse me if this is commong knowldge but I have searched hard and couldn't find anything helpful, relevant or understandable(Which is odd considering I'm a C developer!).
My question is "How can I make java print a Unicode string in the windows shell ?". For simplicity say I have the hello world code in another language(ex: "سلام") and I want to display it in the shell (actually i want to get Unicode too, but first I have to figure this one out).
This works perfectly in Intellij IDEA without any extra lines of code!
System.out.println("سلام");
but doesn't work in shell.
I'm seriously disappointed, I migrated from C just to get a better deal with Unicode!
I used Intellij IDEA/Java 1.8 on Windows 10 and tried a bunch of things in a somewhat disorganized manner, but have it almost working. First, here's the code:
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class Java901App {
public static void main(String[] args) {
//System.out.println("Hello world!");
//System.out.println("سلام");
try{
PrintStream outStream = new PrintStream(System.out, true, "UTF-8");
outStream.println("Hello world!");
outStream.println("سلام");
} catch(UnsupportedEncodingException e){
System.out.println("Caught exception: " + e.getMessage());
}
}
}
Note that the encoding for the PrintStream is set to UTF-8. See the selected answer for this post: Chinese Characters Displayed as Questions Marks in Mac Terminal
I added Arabic Script Supplemental Fonts to Windows based on this article from Microsoft: Why does some text display with square boxes in some apps on Windows 10? I'm not sure whether this was essential, but it definitely did no harm. I uninstalled Arabic Script Supplemental Fonts and nothing changed so this step was not necessary.
Before running the app from the console I called chcp 65001. That was definitely essential even though the PrintStream was defined to use UTF-8, as shown in the screen shot below.
I tried setting different fonts for the Command Prompt window by clicking the icon in the top left of the window, selecting Defaults form the dropdown menu and then clicking the Fonts tab. Some worked (e.g. Consolas) and some didn't (e.g. MS Gothic). Note this comment from a SuperUser post: In order for chcp 65001 to work, you must be using a TrueType font in the command prompt.
Here's some sample output:
So it is working except that the characters in the text you provided are being rendered in reverse order. Does anyone know how to fix that, presumably by somehow specifying in the Java source that the text is for a right-to-left language?
Update:
I amended the code so that the Persian text is rendered correctly in the Command Prompt window, though a side effect is that it no longer renders correctly when the code is run within the IDE. Here's the revised code:
public static void main(String[] args) {
try{
StringBuilder persianHello = new StringBuilder("سلام");
PrintStream outStream = new PrintStream(System.out, true, "UTF-8");
outStream.println("Hello world!");
outStream.println(persianHello); // Renders backwards in console, but correctly in the IDE.
byte directionality = Character.getDirectionality(persianHello.charAt(0));
if (directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC) {
outStream.println("Reversed string:" + persianHello.reverse()); // Renders correctly in console, but backwards in the IDE...
}
} catch(UnsupportedEncodingException e){
System.out.println("Caught exception: " + e.getMessage());
}
}
And here's the Command Prompt output using that code:
This fix is a hack; what's really needed is code that will behave correctly regardless of whether it is run from the IDE or the Command Prompt.
I'm making a multi-player game where each player has their own input/output console on the screen. I'm having a bit of trouble trying to do this. I don't want every player to see other player's in/outputs.
To use an analogy, I want to do something like playerOneConsole.out.println("Player One String");, instead of System.out.println("Player One String"); where everyone can see player one's stuff.
After reading some documentation, I've tried this, but it does not work as intended as it throws a NullPointerException:
public class Player {
String myName;
Console myConsole;
public Player(String name) {
myName = name;
myConsole = System.console();
}
public void takeTurn(String playerOptions){
myConsole.writer().print(playerOptions); //This is not right.
}
}
I want playerOptions to print exclusively to that player's console, not the System console.
By the way, I'm using NetBeans IDE 8.0.2 if that makes a difference.
When using java.io.Console, you must execute the Java app from a console e.g. Windows CMD or Linux Terminal. Most IDEs won't execute Java through a console, so System#console returns null.
By the way, this code:
myConsole.writer().print(playerOptions); //This is not right.
It's right indeed :)
I have recently gone from coding java in emacs to doing it in sublime text 2. I would like to know how to create key bindings such that if I type sop I'll get System.out.println() and if I type psvm I'll get public static void main. I'm using Sublime Text 2 for Ubuntu.
Thanks in advance!
You can use snippets.
Tools/New Snippet...:
<snippet>
<content><![CDATA[
System.out.println()
]]></content>
<tabTrigger>sop</tabTrigger>
<scope>source.java</scope>
</snippet>
Save this file with .sublime-snippet extension, in Packages/User.
This way, in Java source files, when you type sop, followed by tab, you'll get what you need.
You can do the same for psvm.
Sure you can use snippets, but for what you're asking, you can just type:
pl[tab] for
System.out.println();
and main[tab] for
public static void main(String[] args) {
}
I'm trying to introduce internationalization support for my app...I created it as a netbeans java desktop application. Netbeans automatically introduced the following code :
public class ABC extends FrameView{
//constructor
public ABC(Singleframeapplication app)
{
//introduced by netbeans automatically
ResourceMap resourceMap=getResourceMap();
//
}
}
how do i used this resourcemap object to set the locale(eg. FR) for my entire app?
PS:i have created ABC_FR.properties in /ABC/resources folder
Thanks
I made this work by adding a call to Locale.setDefault() in the main of my app:
public static void main(String[] args) {
System.out.println(Locale.getDefault()); // the JVM defaults to es_ES on my machine, so this prints "es_ES"
Locale.setDefault(Locale.ENGLISH); // set it to English
System.out.println(Locale.getDefault()); // Now it prints "en"
launch(MyNiceApp.class, args); // my app comes up in English now
}
This is documented relatively well here: http://java.sun.com/developer/technicalArticles/J2SE/locale/
The problem with the documentation is that it discusses in detail the most complicated cases, and glosses over the most simple case of putting the whole application in a specific language, which would seem to be what most programmers would want.