简体   繁体   中英

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!

  1. 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 .

  2. 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 contexts 1 . For example, from a typical command shell.

  3. 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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM