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

Recall that Java\'s Character class is the wrapper class for the primitive type

ID: 3922046 • Letter: R

Question

Recall that Java's Character class is the wrapper class for the primitive type char. The Character class contains a static method called isDigit, which returns true exactly when its parameter is a digit, i.e., one of 0,1,2,...,8,9. Use the isDigit method to create a method called checkForDigit, which is passed a String as parameter and returns true if any digit -- 0,1,2,...,8,9 -- is present in the String. Otherwise the method should return false. Example: "now is the time" -> false Example: "my name is r2d2" -> true Example: "I was number three" -> false Remember that because isDigit is static, you must specify the name of the class, and not an object, when you apply the method (e.g. Character.isDigit('a')). Hint: Your only job here is to fill in the test condition of the if statement. Java API public boolean checkForDigit(String s) { boolean b = false; for (int i = 0; i < s.length(); i++) {

Explanation / Answer

public boolean checkForDigit(String s)
{
boolean b = false;
for (int i = 0; i < s.length(); i++) {
if(Character.isDigit(s.charAt(i)))
{
b = true;
break;
}
}
return b;
}