I'm trying to figure out this problem. The directions are set hasDigit to true when a three character passCode from a scanner contains a digit.
Code below
import java.util.Scanner;
public class CheckingPasscodes {
public static void main (String [] args) {
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.");
}
}
}
I've entered in the line hasDigit = Character.isDigit(passCode);
My logic is that character.isDigit is checking the passCode from the scanner, but I keep getting an error.
I've also tried: hasDigit = Character.isDigit(0); and the first test passes, but not the other. From this I assumed that I would have to enter in the string so it would test for anything, not just the character at position 0.
Thanks for the help.
isDigit() function of Character class excepts either one character or an integer. It does not excepts string , that's why you are getting error. You can modify your code to -
this will give you required result.