Like the title says, I am trying to check two different indices to see if they match. In my case I am checking indices 3 and 4. I have no problem determining if they match or not, but if my string is too short and does not have a character in at index 3 or 4 my program throws a fit. Here is a copy of my code. Can anyone tell me what I am doing wrong?
/** * Checks the characters in a String at indices 3 and 4 to see if those characters match * @param input String to check * @return "MATCH" if String's indices 3 and 4 have the same letter, "NO MATCH" if String's * indices 3 and 4 do not have the same letter, "N/A" if the String is too short to have a letter at index 3 or index 4 */ private static String method4(String input) {
Scanner scan = new Scanner(System.in);
System.out.println("What is your word? ");
input = scan.nextLine();
int len = input.length();
if (len < 4) {
System.out.println("N/A");
}
else{
char char1 = input.charAt(3);
char char2 = input.charAt(4);
if ((char1) == (char2)) {
System.out.println("MATCH");
}}
else
System.out.println("NO MATCH");
Aucun commentaire:
Enregistrer un commentaire