2

I'm trying to make all the strings that contains a capital letter from the input text file to appear in green color in the output console. E.g., input text file contains "I'm used to speak in English." the output should be the whole sentence from the input text file "I'm used to speak in English." with the strings "I'm" and "English" displayed in green color. But I only manage to display the strings "I'm" and "English" in green color without displaying the whole sentence on the console.

Can anyone help to solve this? My code:

    public static void main(String[] args) throws FileNotFoundException {

    Scanner sc = new Scanner(new File("Testing.txt"));

    while(sc.hasNext()){
        String line = sc.nextLine();
        String arr [] = line.split(" ");

        StringBuilder out = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            if (Character.isUpperCase(arr[i].charAt(0))) {
                out.append(arr[i]);
            }
        }

        if (out.length() > 1) {
            out.setLength(out.length() -2);
        }

        System.out.println ("\u001B[32m " + out);
    }
} 
Giovanni Lovato
  • 2,183
  • 2
  • 29
  • 53
K.W
  • 65
  • 1
  • 7
  • 2
    maybe it will help http://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println ? – Roma Khomyshyn Mar 14 '17 at 14:01
  • I manage to print out those capital letter words in color, but I wish to print the whole sentence out with those capital letter words in color only... – K.W Mar 14 '17 at 14:11

2 Answers2

2

Try out something like this:

public static void main(String[] args) throws FileNotFoundException {
    Scanner sc = new Scanner(new File("Testing.txt"));

    while(sc.hasNext()){
        String line = sc.nextLine();
        String arr [] = line.split(" ");

        StringBuilder out = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            if (Character.isUpperCase(arr[i].charAt(0))) {
                out.append("\u001B[32m").append(arr[i]).append(" ");
            } else {
                out.append("\u001b[0m").append(arr[i]).append(" ");
            }
        }

        System.out.println (out);
    }
}

The idea is to store not only the words with the first uppercase letter, but all the words and use \u001b[0m escape sequence to reset formatting for the other words.

Stanislav
  • 27,441
  • 9
  • 87
  • 82
2

When you colorize with ANSI colors, you are supposed to reset colors once you're done with the colored part of the string, or the whole string will have the same color.

The following example uses a colorizeString method that sets the green color to the passed String, and resets the color afterwards (sequence "\u001B[0m") :

public static void main(final String[] args) throws FileNotFoundException {

     Scanner sc = new Scanner(new File("Testing.txt"));


    while (sc.hasNext()) {
        String line = sc.nextLine();
        String arr[] = line.split(" ");

        StringBuilder out = new StringBuilder();
        for (String element : arr) {
            if (Character.isUpperCase(element.charAt(0))) {
                colorizeString(out, element);
            }
            else{
                out.append(element);
            }

            // add the previously removed space
            out.append(" ");
        }

        System.out.println(out);
    }
}

private static void colorizeString(final StringBuilder builder, final String str) {

    builder.append("\u001B[32m");// GREEN foreground
    builder.append(str);
    builder.append("\u001B[0m");// Reset colors

}

Also read the topic mentioned by Roma Khomyshyn : How to print color in console using System.out.println?

Community
  • 1
  • 1
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • Yes, you are right! I never think of reset colors for other texts, now I got it. Thanks for ur help! – K.W Mar 14 '17 at 14:20
  • Just edited and modified The `colorizeString` method and its call, to avoid creating multiple `StringBuilder` objects. – Arnaud Mar 14 '17 at 14:30