Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Set hasDigit to true if the 3-character passCode contains a digit. for java publ

ID: 670790 • Letter: S

Question

Set hasDigit to true if the 3-character passCode contains a digit.

for java

public class CheckingPasscodes {

public static void main (String [] args) {

boolean hasDigit = false;

String passCode = "";

int valid = 0;

passCode = "abc";

//solution goes here

if (hasDigit) {

System.out.println("Has a digit.");

}

else {

System.out.println("Has no digit.");

}

return;

}

}

Set hasDigit to true if the 3-character passCode contains a digit.

for java

public class CheckingPasscodes {

public static void main (String [] args) {

boolean hasDigit = false;

String passCode = "";

int valid = 0;

passCode = "abc";

//solution goes here

if (hasDigit) {

System.out.println("Has a digit.");

}

else {

System.out.println("Has no digit.");

}

return;

}

}

Explanation / Answer

public class CheckingPasscodes {
public static void main (String [] args) {
boolean hasDigit = false;
String passCode = "";
int valid = 0;

passCode = "abc0xyz";

String number = "0123456789";
char c;
for(int index = 0;index < passCode.length();index++)
{
c = passCode.charAt(index);
if(number.indexOf(c) != -1)
{
hasDigit = true;
}
}

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

return;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote