简体   繁体   English

Character.isDigit() 错误:找不到适合 isDigit(String) 的方法

[英]Character.isDigit() error: no suitable method found for isDigit(String)

Kepp getting an error when using Character.isDigit() Kepp 在使用 Character.isDigit() 时出现错误

I've looked it up elsewhere and tested it fine there but I keep running into this error here.我在别处查过它并在那里测试得很好,但我一直在这里遇到这个错误。

  Scanner scnr = new Scanner(System.in);
  boolean hasDigit;
  String passCode;

  hasDigit = false;
  passCode = scnr.next();

  hasDigit = Character.isDigit(passCode);

  if (hasDigit) {
     System.out.println("Has a digit.");
  }
  else {
     System.out.println("Has no digit.");
  }

Expect true or false depending on scanner input.根据扫描仪输入期望 true 或 false。 Keep getting this error thrown at me:不断向我抛出这个错误:

CheckingPasscodes.java:12: error: no suitable method found for isDigit(String)
  hasDigit = Character.isDigit(passCode);
                      ^
method Character.isDigit(char) is not applicable
  (argument mismatch; String cannot be converted to char)
method Character.isDigit(int) is not applicable
  (argument mismatch; String cannot be converted to int)

The method Character.isDigit() takes a char as input - you are trying to pass it a String . Character.isDigit()方法将char作为输入 - 您试图将其传递给String

The error described what the problem is:该错误描述了问题所在:

argument mismatch;参数不匹配; String cannot be converted to char字符串不能转换为字符

The Scanner.next method will return whole tokens (usually words) from the input stream. Scanner.next 方法将从输入 stream 返回整个标记(通常是单词)。 These words are Strings.这些词是字符串。 The Character.isDigit function requires a char as input, not a String. Character.isDigit function 需要一个字符作为输入,而不是字符串。

You could loop over the word, get each letter as a char and test them:您可以遍历单词,将每个字母作为 char 并测试它们:

for (int i = 0; i < passCode.length(); i++){
    char c = passCode.charAt(i);
    if (Character.isDigit(c)) {
        hasDigit = true;
    }
}

The error is that hasDigit = Character.isDigit(passCode);错误是hasDigit = Character.isDigit(passCode); Character.isDigit() expects a character as an argument but you passing String. Character.isDigit()需要一个字符作为参数,但您传递的是字符串。 So correct this convert the String to char.所以更正这将字符串转换为字符。 you can try你可以试试

     Scanner scnr = new Scanner(System.in);
  boolean hasDigit;
  char passCode;

  hasDigit = false;
  passCode =  scnr.next().charAt(0);

  hasDigit = Character.isDigit(passCode);

  if (hasDigit) {
     System.out.println("Has a digit.");
  }

  else {
     System.out.println("Has no digit.");
  }

I found this worked for me.我发现这对我有用。 I set each specified index in the string to a character value using charAt().我使用 charAt() 将字符串中的每个指定索引设置为一个字符值。 From there I created an if statement that would set hasDigit to true if any of the char variables had a number, using Character.isDigit() enter image description here从那里我创建了一个 if 语句,如果任何 char 变量有一个数字,它将 hasDigit 设置为 true,使用 Character.isDigit() enter image description here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM