0

I want to change the color of the input string into the green. User when type, it will be in the green color. I don't want to first enter the string and then change the color. Can anyone tell how to do this?

System.out.print("Please enter a sentence: ");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();   // I want to change the color to green when user typing the string

output:

  • What IDE are you using? – Sweeper Dec 05 '20 at 09:00
  • NetBeans. But I don't want my code specific to any IDE. – bhishm sharma Dec 05 '20 at 09:07
  • What is your standard output when you run your code? Is it a terminal window? What platform are you on? Is it Windows? If you are on Windows, then do you open a [command prompt](https://www.howtogeek.com/235101/10-ways-to-open-the-command-prompt-in-windows-10/) and run your code? – Abra Dec 05 '20 at 09:07
  • @Abra My standard output is just a normal output window. I'm using Netbeans IDE but I want my code to work the same on all platforms and IDE. I'm using windows. – bhishm sharma Dec 05 '20 at 09:09
  • @Abra I mean console window or output screen in Netbeans – bhishm sharma Dec 05 '20 at 09:09

1 Answers1

0

You can change the input color the same way you change the output color, using ANSI escapes. Here's a post specific to Java.

The trick is to not reset it back to normal (\u001b[0m) before you read user input. For example:

System.out.print("Please enter a sentence: \u001b[92m"); // Note the \u001b[92m after the prompt
Scanner s = new Scanner(System.in);
String name = s.nextLine();
System.out.println("\u001b[0mOther output"); // note the reset \u001b[0m

This may not work on your IDE's console, because some IDEs apply a separate color (which you can set in the settings) to all console inputs. So you should run it in your computer's command line (e.g. cmd, Terminal.app etc).

enter image description here

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Hey, You have not understood my question. I want to change the color when the user entering the string. In short, just we cannot use any color codes inside System.out.print. Because that is output and I want to do it when the user will be inputting. – bhishm sharma Dec 05 '20 at 09:33
  • @bhishmsharma You mean you want each letter of the user input to be a different color or something? – Sweeper Dec 05 '20 at 09:34
  • Yeah. Each user input letter in green color. Click the output link given in the question. You will get a clear understanding of what I want to do. – bhishm sharma Dec 05 '20 at 09:36
  • @bhishmsharma Just output `\u001b[92m` before accepting input. The input will be green! – Sweeper Dec 05 '20 at 09:38
  • Nope. It will not work that way. I tried that already. – bhishm sharma Dec 05 '20 at 09:40
  • @bhishmsharma Pics or it didn't happen. And look, I updated my answer with the output in your screenshot. Try it in your computer's console, rather than NetBeans. – Sweeper Dec 05 '20 at 09:43
  • Okay lemme run in the console. Thanks. – bhishm sharma Dec 05 '20 at 09:53
  • Nope, Its not working on windows command prompt. – bhishm sharma Dec 05 '20 at 14:04
  • @bhishmsharma *Its not working on windows command prompt* - did you actually read the information in the "ANSI escapes" link? It specifically states it doesn't work in Windows command prompt. – camickr Dec 05 '20 at 16:22
  • @camickr No, I didn't read the ANSI escapes information. I just tried the solution provided to me. I was thinking of some way to get the console properties somehow and then change the input color to green programmatically. Do you know anything about this? – bhishm sharma Dec 06 '20 at 07:05
  • @bhishmsharma, *I didn't read the ANSI escapes information.* - well, when someone takes the time to make a suggestion, the least you can make the effort to follow links and understand the solution, instead of asking follow up questions that have already been answered. One again if you read the link it provides an alternative. Read the link!!! You learn by reading. – camickr Dec 06 '20 at 15:43