【问题标题】:how to allow integer only AND not allowing more than 9 digits如何只允许整数和不允许超过 9 位数字
【发布时间】:2021-02-07 03:43:11
【问题描述】:
        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: ");
    }
    

尝试了另一种方法

        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);

你可以看到它根本不起作用,特别是如果你输入一个非整数并且它要求输入两次我很抱歉它一团糟

编辑:好的,有没有不使用正则表达式的方法?我的讲师还没有教过它,所以我不确定我是否允许使用正则表达式

【问题讨论】:

  • 请注意,您的 'while (valid=false)' 将永远无法工作,因为您使用的是单个 '=' 。使用单个“=”,您将值“假”分配给变量“有效”。您要做的是检查值,这是通过'while(valid == false)'实现的,可以简化为'while(!valid)'。
  • 电话号码就是号码是一种误解。它实际上是一串数字。当电话号码以 0 开头时,您可以识别差异。最好在程序中将它们表示为字符串。

标签: java if-statement while-loop integer do-while


【解决方案1】:

您需要使用正则表达式。看看 https://www.w3schools.com/java/java_regex.asp

然后尝试一些类似的东西......

...
final boolean isValid = inputValue.match(^[0-9]{1,9}?) // 1 to 9 digits
if (isValid) {
  ...
}
...

【讨论】:

  • 无效的 Java。 --- 那 ? 应该是 $ 吗?
  • @Andreas,请花点时间看一下regular expressions on java,上面的代码只是一些伪代码,可能无法编译。但它会让你知道你在寻找什么。
  • 所以我是对的,是伪代码,即 无效的 Java。 --- 虽然我同意正则表达式本身是有效的,并且会给出正确的结果,但这没有什么意义。为什么要使量词不情愿 (?)?至少,如果您将? 替换为$,则正则表达式将是一致的,以^ 开头并以$ 结尾,即使它们都是多余的,因为match (应该是matches) 正在做一个完整的“匹配”,而不是“查找”。
【解决方案2】:

我建议这样做:

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】:

    这是我不使用正则表达式的方法

    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);
    

    还有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 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2014-04-11
      • 2021-03-04
      • 1970-01-01
      相关资源
      最近更新 更多