0

I have been asked to Override the method toString of MyClass using StringBuilder and I have to append some text in red so it will look like that:

Name: Tom

Surname: Baker

Age: 17 --> Text in Red

Code:

@Override
public String toString(){

    StringBuilder sb = new StringBuilder;
    sb.append("Name:"+this.name+"\n");
    sb.append("Surname:"+this.surname+"\n");
    sb.append("Age:"+this.age+"\n");

    return sb.toString();
}

Edit

Thank you for your answers.

  • The program is for a university assignment and it is being coded using Eclipse.

  • It is basic Java (not RXJava)

  • The output will be visualised through the console.

  • The teacher ask that but I feel it is not possible.

Community
  • 1
  • 1
Reimond Hill
  • 4,278
  • 40
  • 52
  • 3
    `StringBuilder` and `String` do not have a color per se. The color would have to come from somewhere else, e.g. HTML. Can you add another tag to make your question clear? – Tim Biegeleisen Oct 30 '18 at 08:19
  • Generally there is no such thing as "red text". Text is just a combination of bytes encoded on your screen to represent a sequence of characters. You need to specificy whether you are working with a Rich Text Format or other formats or we won't be able to help you here. – Ben Oct 30 '18 at 08:20
  • Could you specify what kind of program is it? It is a web application or desktop using Swing or JavaFX – Rafał Sokalski Oct 30 '18 at 08:23
  • Expanding on @TimBiegeleisen's comment, how to colorize the text (and whether that's possible at all) depends entirely on the output mechanism. If it's going to be printed on a terminal, ANSI escape codes are the way to go. If it's going to be rendered in a browser, use html (and make sure nothing is escaped). If it's Swing of JavaFX, html can also be used depending on how exactly the text is passed to the UI. – OhleC Oct 30 '18 at 08:24
  • 1
    https://stackoverflow.com/questions/1448858/how-to-color-system-out-println-output Also check this please – Rafał Sokalski Oct 30 '18 at 08:25
  • Thank you very much. Does my edit help? – Reimond Hill Oct 30 '18 at 08:32
  • When using eclipse, text sent to `System.err` is normally printed in red. You might want to use the StringBuilder to compose name and surname and print the age to stderr. – Robert Kock Oct 30 '18 at 08:42
  • stderr? How do I do that? and If I have to add another line after age? – Reimond Hill Oct 30 '18 at 08:48

3 Answers3

1

Since text printed on System.err is normally reported in red, you might try this:

public class MyClass
{
  .....

  @Override
  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    sb.append("Name: " + name);
    sb.append(System.lineSeparator());
    sb.append("Surname: " + surname);
    return (sb.toString());
  }

  public void printAge(PrintStream stream)
  {
    stream.println("Age: " + age);
  }

} // class MyClass

...
MyClass my_instance;
my_instance = new MyClass();
...
System.out.println(my_instance);
System.out.flush() // make sure everything's printed before doing something on stderr
my_instance.printAge(System.err);
System.err.flush() // make sure everything's printed before doing something on stdout again
System.out.println("Anything else to print");
...
Robert Kock
  • 5,795
  • 1
  • 12
  • 20
0

If the output is going to a colour terminal emulator, then you can insert the relevant control codes. There are mechanisms to determine terminal types, such as environment variables ($TERM) and telnet uses a negotiation system (WILL/WONT/DO/DONT, IIRC), but for your purposes you can probably just make the assumption.

Typically a baseline would be vt100 terminal emulation with "ANSI colors". Google for the codes. I get http://wiki.bash-hackers.org/scripting/terminalcodes.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
0

You can find all the info you need in the comments you have been given.

Look here if you want a simple wrapup:

On an ANSI terminal (Linux eg.), you could achieve that by inserting ANSI Escape sequences

System.out.println("Hello \033[31mWorld\033[0m!");

But that won’t work on a Windows console.

On a Windows console, you’ll have to use a third party library, like JAnsi

import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole;

public class Main {
    public static void main(final String[] args) {
        AnsiConsole.systemInstall();
        System.out.println(Ansi.ansi().render("Hello @|red World|@!"));
        AnsiConsole.systemUninstall();
    }
}
Sxilderik
  • 796
  • 6
  • 20