How do you print out a black box char in Java? - java

I thought this would print out a black box: System.out.print('\u25A0');
But for some reason this only prints out question marks.

The console window has to use a TrueType font in order to display Unicode. Unix can show it but if you are using Windows console, this console doesn't use font substitution, there's little you can do to make them show up. If you're using IDE like IntellijIdea inside windows, it doesn't treat console output as a grid of character cells, but rather a text stream (as is usual in Unix, but not Windows) and displays it accordingly.

Related

System.err.print(ln) on the same line as a system.out.println? JAVA

I have a CLI game that I have to make for a uni project and (I know its bad practice) want the lives to be printed in red. I was planning on using the System.err.print to print the hearts in red which works, however they won't join onto my System.out.print("") output.
Is this possible or can I provide some sort of override func?
When using the code;
System.out.print("Player Lives: ");
System.out.print("♥ ♥ ♥ ♥ ♥") My output will be on the same line as expected. However, when I change the code to: System.out.print("Player Lives: ");
System.err.print("♥ ♥ ♥ ♥ ♥") the output will be split on to two seperate lines...
Netbeans IDE v12.0...
any help or ideas would be greatly appreciated!
Thank you!
If the hearts don't represent an error, don't print them on an error stream.
You want the hearts in red, well that is pretty easy to do. You need to learn a bit about consoles (not too much) and what it means to print in color on a console.
Consoles are programs that replaced phyiscal devices. In the old days, a console was something like a television screen hooked up to a communications channel. This TV screen had a keyboard too. The console was responsible for sending information to the "computer" and for receiving data from the computer (console control codes) and presenting that information.
Most of the data sent and received was text. Today, we simulate the console with a program, and the "tty device" is the simulation of the communications channel (a device file today, instead of a modem or serial line). When you type, the data is written to the "tty" attached to your console, and when the computer displays text, the console reads the tty and shows whatever it read.
The reason this is important for color is because you need to print console control codes in your output. The first console control code is to shift the console into displaying red text, the second is to shift the console into displaying "normal" text. With a little more research, you can display (easily) 16 different colors, combined with 16 different background colors, flashing text, etc. Some consoles support more "modes" of operation, you can experiment with yours. There is a standard set of modes, widely supported by nearly all consoles, called ANSI modes.
The default information to a console is text, which is then displayed as text. To get a console to accept a command, it will not be plain text.
public static final String RED = "\u001B[31m";
public static final String RESET = "\u001B[0m";
the first string will shift the console into red text, the second will reset the color mode to default.
So to print a red "hello" you would then do:
System.out.println(RED + "hello" + RESET);
And you could make a function to print red stuff
public void printRed(String message) {
System.out.println(RED + message + RESET);
}
or a function to build a "wrapped" red string, etc.
The simple answer to this is: Just don't do it!
When you send output to System.out and System.err, you have no control over how the output is going to interleave when displayed on a "console". The interleaving is going to depend on a number of factors that are outside of the control of a Java program.
You may be able to make use of your IDE's "feature" of displaying System.err output in red on its console window, but you will find that it doesn't work in other contexts1. For example, from a typical command shell.
It is often possible to get a console to output text in different colors. However this is relying on the console to support (typically) ANSI Escape Codes. These are not necessarily enabled on your end user's console / terminal / terminal emulator. If they are not enabled, the user gets a bunch of weird stuff on their screen.
There are ways to deal with this (e.g. using a Java terminal library), but there will always be rough edges. For example, if your Lecturer / TA runs your app, captures the output and tries to view it with (say) less, they are likely to see weird stuff rather than colors.
My advice: unless it is a specific assignment requirement to output colorized text (and dingbat characters like "♥") ... don't do it. You are making your assignment more difficult, and setting yourself for losing marks if you don't get it right in the context that the lecturer / ta uses to run and mark your code.
(You might argue that this is a useful thing to learn to do. My counter is that it is a lot less useful than you would think. Most applications these days use a GUI framework or a web browser to implement their user interface. Console based interfaces are typically viewed as old fashioned.)
1 - I note your comment that your lecturer is expecting to be given the entire NetBeans project, and will run the code in the same IDE.

Unicode arrow character prints on Mac but not Windows

I have a Java program that is meant to print a Unicode LEFTWARDS ARROW character, ←:
public class test {
public static void main(String[] args) {
System.out.print("\u2190");
}
}
When I run this program in Eclipse on a Mac computer, the console prints the proper arrow symbol, ←. However, when I run the exact same code on a Windows 10 machine, the Unicode character prints as a question mark, ?.
I also tried printing an arrow character in a C++ program on my Windows computer, and got the same Unicode rendering problem.
I don't know when this stopped working and if it is a recent Windows update that is causing the character to not display properly, but I do know that this code used to work and doesn't anymore.
Any ideas as to why this code is not rendering the correct Unicode character on Windows?
Update
To address #ElliotFrisch's comment, I checked which font my Eclipse console is using, and it is using Consolas. I opened the font Character Map in Control Panel, and Consolas does seem to have a leftwards arrow, U+2190 character:
I also tried changing the font to different common fonts like Times New Roman, Calibri, Cambria and Arial, but none of those printed the correct arrow character.

ANSI character not working for eclipse console on Ubuntu Linux

I am fairly new to Java and need your help. I am trying to change the color of the text in Eclipse console on run-time. I referred to one of the posts and tried to follow that. But the output shows a box of unicode escape character and the color of the text remains unchanged.
public static void main(String[] args)
{
System.out.println((char)27 + "[31mTest red color");
}
OUTPUT:
|00|
|1B| [31mTest red color
Notes:
For output, I couldn't upload the image so i have typed [001B].
The console uses UTF-8 encoding.
UPDATE
Following the comment from Jarrod for possible duplication of the question, here is my update -
The question is mainly for ANSI character not working and not for text color, so i have updated the question.
Eclipse does not support ANSI escape characters out of the box.
You have to install a plugin for that: https://marketplace.eclipse.org/content/ansi-escape-console

Print unique ascii characters in eclipse console

Kind of a strange question but... here it goes.
Recently my application threw an IOException that the text only had a clubs symbol in it (like the suit in cards) I know this is probably because there was a number in there that was cast to a char and printed to the screen, and I've found where that might have happened. The only problem is, I can't recreate it in eclipse because the eclipse console doesn't want to print those characters for me. All I get are boxes.
I figure this is an encoding issue or something but I need eclipse to print out those characters just like the windows console would. Is there a setting I can change to do this?
The respective Unicode character is U+2663. Just print "\u2663" and you should be fine. This has nothing to do with ASCII, though.
If you get boxes it may also be a font issue. If the font you selected for the console view in Eclipse does not have a glyph for that code point you'll get boxes, usually. The character might still correctly printed, though. Usually monospaced fonts have that character, though, since it was historically part of the glyphs for the control characters below character code 32 (not that control characters ever had the intention of a visual appearance, but well, they could be in the screen buffer, so someone thought it would be a good idea to display them as well).

Eclipse console exception color

This seems simple enough, but I can't seem to find the setting in Eclipse to change this setting.
I prefer my consoles to have dark colors with white text. I leave the print to stderr as the red default.
My problem lies in when I receive an Exception, it's set to a very dark blue, so I can't see the text of it easily without highlighting the link.
I didn't see it in the console preferences, and also do not see it anywhere in the various other setting locations.
Changing the Hyperlink text color will do what you want, but this is used elsewhere in Eclipse so you may want to choose a colour that is reasonably legible on both light and dark backgrounds.
Does Hyperlink text color do the trick?
"General"->"Appearance"->"Colors and Fonts", "Hyperlink text Color"?
I like this:
Console Grep
It allows for custom highlighting/text colors depending on what is printed. Assuming you have an idea of how regexes work (if not lots of tutorials exist) it works quite well.

Categories

Resources