6

I have the following statements inside my class:

String myName = "Joe";
System.out.println("My name is " +myName);

I need the value on the variable myName to be printed as italic text.

7
  • 6
    Well does your console support italicized text? We have no idea where you're even trying to run it. You should normally just consider the console as a simple console output device.
    – Jon Skeet
    Commented May 18, 2015 at 18:14
  • 1
    stackoverflow.com/questions/26132541/eclipse-console-styling This link will help you in this regard. System.out.println("\030[3mMy name is\030[0m"); Commented May 18, 2015 at 18:15
  • this is a class exercise.
    – LH7
    Commented May 18, 2015 at 18:16
  • 3
    Are you trying to apply style in the console output or you are trying to use it with swing or JSP? Commented May 18, 2015 at 18:16
  • 1
    @Hurix That's not going to work in general. For instance, in my eclipse console it just prints out garbage characters.
    – azurefrog
    Commented May 18, 2015 at 18:17

2 Answers 2

11

Try:

System.out.println("\033[3mText goes here\033[0m");

Which will output italic text if your console supports it. You can use [1m for bold, etc. Play around with the different values of [nm.

-2

Here is an example of how to do that:

import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;


public class Foo
{
public static void main(String[] args) throws IOException
{
PrintWriter out = new PrintWriter(new FileWriter("myFile.html"));
out.println("<u><i>my output</i></u>");
out.flush();
out.close();
}
}
1
  • It should be in the console.
    – Sybren
    Commented May 18, 2015 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.