How to use Scanner to accept only valid int as input

后端 未结 6 1260
梦谈多话
梦谈多话 2020-11-22 05:53

I\'m trying to make a small program more robust and I need some help with that.

Scanner kb = new Scanner(System.in);
int num1;
int num2 = 0;

System.out.prin         


        
6条回答
  •  情歌与酒
    2020-11-22 06:27

    What you could do is also to take the next token as a String, converts this string to a char array and test that each character in the array is a digit.

    I think that's correct, if you don't want to deal with the exceptions.

提交回复
热议问题