0

I'm doing a class exercise about reserved java keywords, and I want to change the color in the input of a scanner. Happy to hear any suggestion!

What I'm trying to switch

Here's my code if you want to take a look.

while (repetir || contador == max) {
            String palabras = new Scanner(System.in).nextLine().toLowerCase();
            
            if (palabras.contains(".")) {
                System.out.println("Has acertado las siguientes palabras: "
                        + acertadas);
                repetir = false;
            } else {
                if(reservadas.contains(palabras)) {
                    palabras = bien + palabras; //This is my attempt to switch the color
                    acertadas.add(palabras);
                    contador++;
                } else {
                    System.out.println(mal + "No es correcto" + ANSI_RESET);
                }
                if (contador == 50) {
                    System.out.println("ENHORABUENA!!!");
                    System.out.println("Has acertado todas las palabras "
                            + "reservadas");
                    repetir = false;
                }
            }
            
        }

When I press the intro keyword and the word writed in the scanner is correct, I want to switch the color to green.

  • A similar question has been answered here: https://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println – Lorenz Hetterich Nov 22 '22 at 16:14
  • Thank's for your answer, but my objective is to switch the color in the input of the scanner if the word writed is a reserved java keyword, if not I print "No es correcto" in red. – Ivan Parra Nov 22 '22 at 17:00
  • your attempt of concatenating the Strings should work. Make sure to also put an ANSI_RESET when the color should end. Your code is missing some important details (e.g., what type is acertadas) to find out why it is not working. – Lorenz Hetterich Nov 22 '22 at 17:06
  • 1
    oh sorry I am completly wrong and I misunderstood your question. I believe that what you are trying to do is not possible (at least not in a generic way) using simple java IO. The only way it seems possible to me is by writing your own GUI. – Lorenz Hetterich Nov 22 '22 at 17:08

0 Answers0