I have a problem with the following code. The scenario is as such: the user inputs a string (can be a sentence). The code will, for each words, print the complete word + "ay" at the end if it starts with a vowel, and puts the first letter of the word at the end of the word (and then adds "ay" at the end). If it is not a letter that starts the word, then just print it. However, it is supposed to ask if the user wants to continue, by inputing "Yes" or "No", with the correct capitalization.
I try to debug it, and does not work as intended. You will see that everything within the while (scanWord.hasNext()) works (and everything before). However, after that loop, the rest gets ignored by the executer (will be commented as such). Can you help me get an answer, explain the mistake, and therefore make others and myself better java developers?
//import Scanner
import java.util.Scanner;
public class Prog508a {
public static void main(String [] args) {
//Declare variables
String word;
String response;
boolean answer = true;
//Declare Scanner objects
Scanner scanWord = new Scanner(System.in);
Scanner answerQuestion = new Scanner(System.in);
//while user still wants to continue
while (answer == true) {
//ask user for input
System.out.print("Enter a sentence: ");
//while the input still contains words
while (scanWord.hasNext()) {
//input from user is word
word = scanWord.next();
//if the character is letter at 0
if (Character.isLetter(word.charAt(0))) {
//and if the character is not a vowel at index 0
if (!(word.charAt(0) == 'a')
&& !(word.charAt(0) == 'e')
&& !(word.charAt(0) == 'i')
&& !(word.charAt(0) == 'o')
&& !(word.charAt(0) == 'u')) {
//the output is added the substring of word from the second letter and added first letter at the end
word = word.substring(1,word.length())+ word.charAt(0);
}
//word is added "ay"
word+= "ay";
}
//print the word result
System.out.print(word + " ");
}
//EVERYTHING UNDER HERE DOES NOT EXECUTE
System.out.println("\n");
System.out.print("Do you wish to convert another sentence (Yes or No): ");
response = answerQuestion.next();
while (!(response.compareTo("No") == 1) || !(response.compareTo("Yes") == 1)) {
//if the answer is no or yes (no capitalization)
if (response.compareTo("no") == 1 || response.compareTo("yes") == 1) {
while (response.compareTo("no") == 1 || response.compareTo("yes") == 1) {
System.out.println("Capitalization is important! Input correctly: ");
response = answerQuestion.next();
}
} else if (!(response.compareTo("No") == 1) || !(response.compareTo("No") == 1)) {
while (!(response.compareTo("No") == 1)
|| !(response.compareTo("Yes") == 1)
|| !(response.compareTo("no") == 1)
|| !(response.compareTo("yes") == 1)) {
System.out.println("You must input either yes or no! Input correctly: ");
response = answerQuestion.next();
}
}
}
if (response.compareTo("No") == 1)
answer = false;
if (response.compareTo("Yes") == 1)
answer = true;
}
//close the scanners
answerQuestion.close();
scanWord.close();
}
}