【问题标题】:How do I make sure that the input is not null and that only one character is being entered?如何确保输入不为空并且只输入一个字符?
【发布时间】:2021-08-12 00:15:22
【问题描述】:
public static boolean correctchar(char b) {
    Scanner scan = new Scanner(System.in);
    b = scan.next().charAt(0);
    
    if (Character.toString(b).matches("^[a-zA-Z]") ) {
        System.out.println("True");
        return true;
    } else {
        System.out.println("False");
        return false;
    }
}

我有这种方法可以检查输入是否是字母表中的字母,但我想确保用户的输入不为空并且用户只输入一个字母。例如“A”或“a”是一个正确的字符,问题是如果我输入“Abcdef”那么它仍然是正确的,因为第一个字母仍然是一个有效的字符。我想让它让用户只能输入一个字符,我想我已经通过使用扫描仪和charAt(0) 做到了这一点,但是有没有更有效的方法来做到这一点,我也不确定如何使输入不为空。

【问题讨论】:

  • 您可以检查字符串的长度。 String 类有一个 length() 方法
  • 但变量最初是一个字符,所以当我这样做时,它不会让我检查长度。
  • @john 如果您将b 作为参数传递给方法,您为什么要读取correctchar() 中的输入?您可能需要在调用 correctchar() 之前在调用站点读取输入,然后将读取的字符传递给 correctchar()
  • 我不太清楚你的意思,你的意思是main方法吗?
  • @john 是的,该值将在主方法(或您调用方法correctchar() 的任何地方)中从用户处接受,并且在调用此方法时将传递输入的值:@ 987654331@;

标签: java regex null char boolean


【解决方案1】:

我已经修改了你的代码来做你想做的事:

public static boolean correctchar(char b) {
    Scanner scan = new Scanner(System.in);
    String input = scan.next();
    
    // This checks if the input is null, is empty (i.e., "") or is bigger than one character
    // If any of these conditions are fulfilled, then we return false.
    if (input == null || input.length() != 1) {
        return false;
    }

    b = input.charAt(0);
    
    if (Character.toString(b).matches("[a-zA-Z]") ) {
        System.out.println("True");
        return true;
    } else {
        System.out.println("False");
        return false;
    }
}

编辑

没有扫描仪(见 cmets):

public static boolean correctchar(char b, String input) {        
    // This checks if the input is null, is empty (i.e., "") or is bigger than one character
    // If any of these conditions are fulfilled, then we return false.
    if (input == null || input.length() != 1) {
        return false;
    }

    b = input.charAt(0);
    
    if (Character.toString(b).matches("[a-zA-Z]") ) {
        System.out.println("True");
        return true;
    } else {
        System.out.println("False");
        return false;
    }
}

【讨论】:

  • 我刚刚更新了一些代码:(1) 将input.length 更改为input.length()。 (2) 你可以简单地写input.length() != 1 而不是input.isEmpty() || input.length > 1。 (3) 在[a-zA-Z] 之前不需要^,尽管它没有造成任何伤害。对于matches^(即行首)和$(即行尾)是多余的。
  • 如果我没有扫描仪并通过参数传递它怎么办?
  • 删除与扫描仪相关的行并将所有提及的“输入”替换为“b”
  • 我还在我的答案中添加了一个版本作为参考。
  • @user11350388 - 您的编辑不正确。 John 提出的问题是,如果 input 作为参数传递而不是使用 Scanner 获取它会怎样。您可以简单地将您的编辑回滚到我上次的编辑。另外,请注意b == null 明显错误,因为b 是原始的,因此您不能将其与null 进行比较。
【解决方案2】:
public static boolean correctChar() {
     try (Scanner scan = new Scanner(System.in)) {
         String input = null;
         do {
             input = scan.next();
             if (input != null && input.length() == 1) {
                 boolean isCorrect = input.matches("[a-zA-Z]"); 
                 System.out.println(isCorrect ? "True" : "False");
                 return isCorrect;
             } else {
                 System.out.println("Insert only one character");
             }
         } while (true);
     }
 }

【讨论】:

    【解决方案3】:

    我做了一些改变:

    1. 如果输入无效,请用户重新输入。

    2. 确保关闭扫描仪scan.close()

       Scanner scan = new Scanner(System.in);
      
       System.out.println("Please enter only one character : ");
      
       String input = scan.next();
      
       while (null == input || input.isEmpty() || input.length() > 1) {
           System.out.println("Invaid Input, Please enter only one character : ");
           input = scan.next();
       }
       scan.close();
      
       if (Character.toString(input.charAt(0)).matches("^[a-zA-Z]")) {
           System.out.println("True");
           return true;
       } else {
           System.out.println("False");
           return false;
       }
      

      }

    【讨论】:

      猜你喜欢
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多