0

How do I prevent my code from printing user input?

import java.util.Scanner;
public class Days {
    public static void main(String[] args) {

        Scanner ent = new Scanner(System.in);
        int day;
        System.out.println("Entrez un nombre d'un jour du semaine");
        day = ent.nextInt();
        switch (day) {
        case 1:
            System.out.println("Dimanche");
            break;
        case 2:
            System.out.println("Lundi");
            break;
        case 3:
            System.out.println("Mardi");
            break;
        case 4:
            System.out.println("Mercredi");
            break;
        case 5:
            System.out.println("Jeudi");
            break;
        case 6:
            System.out.println("Vendredi");
            break;
        case 7:
            System.out.println("Samedi");
            break;

        default:
            System.out.println("Nombre donnée invalide");
            break;

        }

    }
}

I'm trying a simple program that shows the days of the week (dimanche, lundi, mardi, etc are the days but in french) it works fine but it prints user input before printing the actual day like if the input is 3 (mardi) then it prints this in the console:

3 
mardi

How do I prevent it from printing the input?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
moh470
  • 1
  • 1
  • You have to use a library like jcurses to avoid echoing user input. Or use AWT, Swing or Java FX (which would be GUI, not TUI). – Elliott Frisch May 22 '23 at 14:39
  • 1
    You sort-of can't, because of the way that the console works. The only simple thing I can suggest is to pretend the user is [entering a "password"](https://stackoverflow.com/questions/8138411/masking-password-input-from-the-console-java). – Andy Turner May 22 '23 at 14:40
  • Another option: [How to clear the console using Java?](https://stackoverflow.com/q/2979383) – 001 May 22 '23 at 14:44
  • 1
    Does this answer your question? [Hide input on command line](https://stackoverflow.com/questions/10819469/hide-input-on-command-line) – OH GOD SPIDERS May 22 '23 at 14:44
  • As a note, it's not printing the value. That is the value the user has typed. If you're looking to prevent the display of the characters the user is typing, you can use a `System.console()` instance. – Reilas May 23 '23 at 02:54

0 Answers0