Problème avec Java Scanner sc.nextLine();

sry sur mon anglais 🙂
Im nouveau à la programmation Java et j'ai un problème avec le Scanner. J'ai besoin de lire un Int, de montrer quelques trucs, puis lire une chaîne donc j'utilise sc.nextInt(); montrer mon stuff showMenu(); et puis essayez de lire une chaîne de caractères palabra=sc.nextLine();

Certains me l'a dit j'ai besoin d'utiliser un sc.nextLine(); après sc.nextInt(); mais je ne comprends pas pourquoi vous avez à faire 🙁

Voici mon code:

public static void main(String[] args) {
    //TODO code application logic here
    Scanner sc = new Scanner(System.in);
    int respuesta = 1;

    showMenu();
    respuesta = sc.nextInt();
    sc.nextLine(); //Why is this line necessary for second scan to work?

    switch (respuesta){
        case 1:
            System.out.println("=== Palindromo ===");
            String palabra = sc.nextLine();
            if (esPalindromo(palabra) == true)
                System.out.println("Es Palindromo");
            else
                System.out.println("No es Palindromo");
        break;
    }


}

Ty beaucoup pour votre temps et de votre Aide 😀

OriginalL'auteur Jonathan B | 2010-03-14