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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.