+-

do{
out.println("\n---------------------------------");
out.println("---------------------------------");
out.print("Please type your acces card number: ");
try{
card = input.nextInt();
if(card.length != 10){
out.println("The number you typed is incorrect");
out.println("The number must be 10 numbers long");
continue;
}
}
catch(InputMismatchException ex){
}
}while(true);
我试图让卡长10个字符.与(1234567890)类似,如果用户输入(123)或(123456789098723),则应显示错误消息. card.length似乎不起作用.
最佳答案
只需将int更改为String
String card = input.next();
if(card.length() != 10){
//Do something
}
您可以稍后将其轻松转换为int
int value = Integer.parseInt(card);
点击查看更多相关文章
转载注明原文:如何设置可以输入的字符数限制? (JAVA) - 乐贴网