英文:
how to allow integer only AND not allowing more than 9 digits
问题
System.out.println("请输入您的手机号码:");
while (in.hasNextLong()) {
long phone = in.nextLong();
if (in.hasNextLong()) {
if (phone < 1000000000) {
System.out.println("手机号码:" + phone);
}
} else if (!in.hasNextInt()) {
System.out.println("请输入有效的手机号码:");
} else if (phone < 1000000000) {
System.out.println("请输入有效的手机号码:");
}
}
尝试另一种方式:
boolean valid;
long phone;
do {
System.out.println("请输入您的手机号码:");
if (!in.hasNextLong()) {
phone = in.nextLong();
if (phone > 1000000000) {
System.out.println("请输入有效的手机号码");
in.nextLong();
valid = false;
}
}
} while (valid = false);
System.out.println("手机号码:" + phone);
正如您所见,这根本不起作用,尤其是如果您输入非整数,它会要求您输入两次,对不起,这很混乱。
编辑:好的,那么是否有一种不使用正则表达式的方法?我的讲师还没有教过我使用正则表达式,所以我不确定是否可以使用正则表达式。
英文:
System.out.println("Enter your phone number: ");
while(in.hasNextLong()) {
long phone = in.nextLong();
if(in.hasNextLong()) {
if(phone < 1000000000) {
System.out.println("Phone number: "+phone);
}
} else if(!in.hasNextInt()) {
System.out.println("Please enter a valid phone number: ");
} else if (phone < 1000000000) {
System.out.println("Please enter a valid phone number: ");
}
tried another way
boolean valid;
long phone;
do {
System.out.println("Enter your phone number: ");
if(!in.hasNextLong()) {
phone =in.nextLong();
if(phone > 1000000000) {
System.out.println("Please enter a valid phone number");
in.nextLong();
valid=false;
}
}
}while(valid=false);
System.out.println("Phone: " + phone);
as you can see it doesnt work at all especially if you input a non integer and it ask for input twice im sorry its a mess
edit: ok so is there a way without using regex? my lecturer hasnt taught it yet so im not sure im allowed to use regex
答案1
得分: 3
你需要使用正则表达式。查看一下这个网页:
https://www.w3schools.com/java/java_regex.asp
然后尝试类似以下的代码:
...
final boolean isValid = inputValue.match(^[0-9]{1,9}?) // 匹配 1 到 9 位数字
if (isValid) {
...
}
...
英文:
you need to use regex. take a look to
https://www.w3schools.com/java/java_regex.asp
and try something along the lines...
...
final boolean isValid = inputValue.match(^[0-9]{1,9}?) // 1 to 9 digits
if (isValid) {
...
}
...
答案2
得分: 1
我会建议这样做:
System.out.println("请输入您的电话号码:");
int phone;
for (;;) { // 无限循环
String line = in.nextLine();
if (line.matches("[0-9]{1,9}")) {
phone = Integer.parseInt(line);
break;
}
System.out.println("请输入有效的电话号码:");
}
System.out.println("电话号码:" + phone);
英文:
I would recommend doing it this way:
System.out.println("Enter your phone number: ");
int phone;
for (;;) { // forever loop
String line = in.nextLine();
if (line.matches("[0-9]{1,9}")) {
phone = Integer.parseInt(line);
break;
}
System.out.println("Please enter a valid phone number: ");
}
System.out.println("Phone number: "+phone);
答案3
得分: 1
这是我不使用正则表达式的方法:
System.out.println("请输入您的电话号码:");
int phone;
int index = 0;
while(true) {
String line = in.nextLine();
if (valid(line)){
phone = Integer.parseInt(line);
break;
}
System.out.println("请输入有效的电话号码:");
}
System.out.println("电话号码:" + phone);
以及 valid()
方法:
boolean valid(String line){
if(line.length() > 9) return false;
for(int i = 0; i < line.length(); i++){
boolean isValid = false;
for(int asciiCode = 48; asciiCode <= 57 && !isValid; asciiCode++){
// 48 是字符 '0' 的 ASCII 数值表示
// ...
// 57 是字符 '9' 的 ASCII 数值表示
// 参考 ASCII 表
if((int)line.charAt(i) == asciiCode){
isValid = true;
}
}
if(!isValid) return false;
}
return true;
}
英文:
That's my approach without using regex
System.out.println("Enter your phone number: ");
int phone;
int index = 0;
while(true) {
String line = in.nextLine();
if (valid(line)){
phone = Integer.parseInt(line);
break;
}
System.out.println("Please enter a valid phone number: ");
}
System.out.println("Phone number: " + phone);
And the valid()
method
boolean valid(String line){
if(line.length() > 9) return false;
for(int i = 0; i < line.length(); i++){
boolean isValid = false;
for(int asciiCode = 48; asciiCode <= 57 && !isValid; asciiCode++){
//48 is the numerical representation of the character 0
// ...
//57 is the numerical representation of the character 9
//see ASCII table
if((int)line.charAt(i) == asciiCode){
isValid = true;
}
}
if(!isValid) return false;
}
return true;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论