In: Computer Science
Set hasDigit to true if the 3-character passCode contains a digit.
public class CheckingPasscodes {
public static void main (String [] args) {
boolean hasDigit = false;
String passCode = "";
int valid = 0;
passCode = "abc";
/* Your solution goes here */
if (hasDigit) {
System.out.println("Has a digit.");
}
else {
System.out.println("Has no digit.");
}
return;
}
}
Program:
Sample output:
if passCode = "ab3";
Code to copy:
public class CheckingPasscodes
{
public static void main (String [] args)
{
boolean hasDigit = false;
String passCode = "";
int valid = 0;
passCode = "abc";
//check each character in the passcode whether
//the passcode has a digit or not
for(int index = 0;index < passCode.length();index++)
{
char charcter=passCode.charAt(index);
//check the character is a digit or not
if(Character.isDigit(charcter))
{
//set the boolean value hasDigit as true, if
// the character is a digit
hasDigit = true;
}
}
//display a message "Has a Digit, if the passcode
//contains a digit
if (hasDigit)
{
System.out.println("Has a digit.");
}
//display a message "Has no digit, if the passcode
//does not contain a digit
else
{
System.out.println("Has no digit.");
}
return;
}
}