While loop that is not working with Try/Catch statements

前端 未结 3 1073
太阳男子
太阳男子 2021-01-24 13:16

Im trying to give the user the opportunity to repeat an input after introducing something that has produced an error but something is not working because once the err i

3条回答
  •  清酒与你
    2021-01-24 13:40

    When you enter a non-integer the Scanner call to nextInt() doesn't consume the non-integer. You need to call keyboard.next() (or keyboard.nextLine()) to consume it. Something like,

    try {
        dim = keyboard.nextInt();
    } catch (Exception e) {
        System.out.printf("%s is not an integer.%n", keyboard.next());
        err = 1;
    }
    

提交回复
热议问题