mercredi 22 janvier 2020

My stops running when I input an integer value for some reason [duplicate]

I'm learning java if statements and I wrote this code: Scanner scan = new Scanner(System.in);

    System.out.println("How old are you?");
    int age = scan.nextInt();

    System.out.println("Cats or dogs?");
    String pet = scan.nextLine();

    if(age > 12 && pet.equals("dogs"))
    {
        System.out.println("Welcome!");
    }

When I run this, the first line prints and I input a number and then the code just stops running for some reason. This is only fixed when I instead parse the string like this:

int age = Integer.parseInt(scan.nextLine());

Why is it doing this?

Aucun commentaire:

Enregistrer un commentaire