1

How to set massage when input is different than number to not provide error "Exception in thread "main" java.util.InputMismatchException" ?

I have to use if/else statement or what ? Please help my in my poor java :)

public static void main(String[] args) {

    System.out.println("Oceń konika od 1 do 6");

    Scanner PodajCyfre = new Scanner( System.in ); //nadanie zmiennej input PodajCyfre


    int WygladKonika = PodajCyfre.nextInt(); // przypisanie PodajCyfre do wartości


    switch (WygladKonika) {

        case 1 :
            System.out.println("Ten konik jest brzydki i mi sie kompletnie nie podoba");
            break;
        case 2:
            System.out.println("Nie jest za ładny ten konik, ale widziałam brzydsze");
            break;
        case 3:
            System.out.println("Taki średni jest ten konik");
            break;
        case 4:
            System.out.println("Ładny konik");
            break;
        case 5:
            System.out.println("Śliczny konik bardzo mi sie podoba");
            break;
        case 6:
            System.out.println("To jest najpiekniejszy konik jakiego widziałam. Uwielbiam go !!");
            break;
        default:
            System.out.println("To nie jest cyfra od 1 do 6");


    }
}

}

3 Answers3

1

Check before cast to Int scanner input.So code will be in while loop for input.

   public static void main(String[] args) {


        Scanner PodajCyfre = new Scanner(System.in); //nadanie zmiennej input PodajCyfre

        System.out.println("Oceń konika od 1 do 6");
        while (!PodajCyfre.hasNextInt()) {
            System.out.println("Please write a number ! Oceń konika od 1 do 6");
            PodajCyfre.next();
        }

        int WygladKonika = PodajCyfre.nextInt(); // przypisanie PodajCyfre do wartości

        switch (WygladKonika) {

            case 1:
                System.out.println("Ten konik jest brzydki i mi sie kompletnie nie podoba");
                break;
            case 2:
                System.out.println("Nie jest za ładny ten konik, ale widziałam brzydsze");
                break;
            case 3:
                System.out.println("Taki średni jest ten konik");
                break;
            case 4:
                System.out.println("Ładny konik");
                break;
            case 5:
                System.out.println("Śliczny konik bardzo mi sie podoba");
                break;
            case 6:
                System.out.println("To jest najpiekniejszy konik jakiego widziałam. Uwielbiam go !!");
                break;
            default:
                System.out.println("To nie jest cyfra od 1 do 6");


        }
    }
drowny
  • 2,067
  • 11
  • 19
0

Read the int as follows

int WygladKonika;
try {
    WygladKonika = PodajCyfre.nextInt();
} catch (InputMismatchException e) {
    System.out.println("Input can't be converted to an integer");
    // exit, read another value, etc
}
0

Use try-catch block to catch java.util.InputMismatchException and show message when this exception is occurred.

try {
    Scanner PodajCyfre = new Scanner(System.in); //nadanie zmiennej input PodajCyfre
    int WygladKonika = PodajCyfre.nextInt(); // przypisanie PodajCyfre do wartości

    switch (WygladKonika) {
        //
    }
} catch (InputMismatchException e) {
    // Exception caught, show message
    System.out.println("Invalid input");
}
benjamin c
  • 2,278
  • 15
  • 25