In: Computer Science
JAVE
3.10.1: String with digit.
Set hasDigit to true if the 3-character passcode contains a digit.
MUST USE THIS TEMPLATE:
public class CheckingPasscodes {
public static void main (String [] args) {
boolean hasDigit;
String passCode;
hasDigit = false;
passCode = "abc";
/* Your solution goes here */
if (hasDigit) {
System.out.println("Has a digit.");
}
else {
System.out.println("Has no digit.");
}
}
}
//The added solution is in bold form.public class CheckingPasscodes {public static void main (String [] args) {boolean hasDigit;String passCode;hasDigit = false;passCode = "abc";//Begin the for loop.for(int i = 0 ; i < passCode.length(); i++){ //Extract a character from passCode //and store it in the variable ch. char ch = passCode.charAt(i); //Check the extracted character //to be digit or not. if(Character.isDigit(ch)) { //Update the variable hasDigit to true. hasDigit = true; }}if (hasDigit) {System.out.println("Has a digit.");}else {System.out.println("Has no digit.");}}}
// CheckPasscodes.java
import java.util.Scanner;
public class CheckPasscodes {
public static void main(String[] args) {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner scnr = new Scanner(System.in);
boolean hasDigit;
String passCode;
hasDigit=false;
passCode=scnr.next();
for(int i=0;i<passCode.length();i++)
{
if(Character.isDigit(passCode.charAt(i)))
{
hasDigit=true;
break;
}
}
if(hasDigit)
{
System.out.println("Has digit.");
}
else
{
System.out.println("Has no digit.");
}
}
}
________________________
Output#1:
Hello5Kane
Has digit.
________________________
Output#2:
HelloKane
Has no digit.