205k views
1 vote
In java please!

Complete the checkCharacter() method which has 2 parameters: A String, and a specified index (int). The method checks the character at the specified index of the String parameter, and returns a String based on the type of character at that location indicating if the character is a letter, digit, whitespace, or unknown character.

Ex: The method calls below with the given arguments will return the following Strings:

checkCharacter("happy birthday", 2) returns "Character 'p' is a letter"
checkCharacter("happy birthday", 5) returns "Character ' ' is a white space"
checkCharacter("happy birthday 2 you", 15) returns "Character '2' is a digit"
checkCharacter("happy birthday!", 14) returns "Character '!' is unknown"

1 Answer

0 votes

Answer:

class Main {

public static String checkCharacter(String str, int index) {

char character = str.charAt(index);

if (Character.isLetter(character)) {

return "Character '" + character + "' is a letter";

} else if (Character.isDigit(character)) {

return "Character '" + character + "' is a digit";

} else if (Character.isWhitespace(character)) {

return "Character '" + character + "' is a white space";

} else {

return "Character '" + character + "' is unknown";

}

}

public static void main(String[] args) {

System.out.println(checkCharacter("happy birthday", 2));

System.out.println(checkCharacter("happy birthday", 5));

System.out.println(checkCharacter("happy birthday 2 you", 15));

System.out.println(checkCharacter("happy birthday!", 14));

}

}

Step-by-step explanation:

This answer was created using AI and it looks very correct to me.

User MQuiggGeorgia
by
7.6k points