如何只允许输入整数,且不允许超过9位数。

huangapple go评论80阅读模式
英文:

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(&quot;Enter your phone number: &quot;);
	while(in.hasNextLong()) {
		long phone = in.nextLong();
		if(in.hasNextLong()) {
			if(phone &lt; 1000000000) {
				System.out.println(&quot;Phone number: &quot;+phone);	
		}
	} else if(!in.hasNextInt()) {
		System.out.println(&quot;Please enter a valid phone number: &quot;);
	} else if (phone &lt; 1000000000) {
		System.out.println(&quot;Please enter a valid phone number: &quot;);
	}

tried another way

		boolean valid;
	long phone;
	do {
		System.out.println(&quot;Enter your phone number: &quot;);
		
		if(!in.hasNextLong()) {
			phone =in.nextLong();
			if(phone &gt; 1000000000) {
		    System.out.println(&quot;Please enter a valid phone number&quot;);
			in.nextLong();
		    valid=false;
			}
		} 
		}while(valid=false);
	System.out.println(&quot;Phone: &quot; + 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(&quot;Enter your phone number: &quot;);
int phone;
for (;;) { // forever loop
	String line = in.nextLine();
	if (line.matches(&quot;[0-9]{1,9}&quot;)) {
		phone = Integer.parseInt(line);
		break;
	}
	System.out.println(&quot;Please enter a valid phone number: &quot;);
}
System.out.println(&quot;Phone number: &quot;+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(&quot;Enter your phone number: &quot;);
int phone;
int index = 0;
while(true) { 
    String line = in.nextLine();
    if (valid(line)){
        phone = Integer.parseInt(line);
        break;
    }
System.out.println(&quot;Please enter a valid phone number: &quot;);
}
System.out.println(&quot;Phone number: &quot; + phone);

And the valid() method

boolean valid(String line){
    if(line.length() &gt; 9) return false;
    for(int i = 0; i &lt; line.length(); i++){
       boolean isValid = false;
       for(int asciiCode = 48; asciiCode &lt;= 57 &amp;&amp; !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;
}

huangapple
  • 本文由 发表于 2020年10月24日 14:53:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64510781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定