-2

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();
    }
}
Darshan Mehta
  • 27,835
  • 7
  • 53
  • 81
PN.Perrin
  • 11
  • 1

2 Answers2

0

You need the correction at two places:

  • First, you need to change while (scanWord.hasNext()) { to if (scanWord.hasNext()) { as you would want the control to print the Yes/No question once the sentence is parsed.

  • Second, you need to correct the string comparison, you need to change response.compareTo("no") == 1 to response.equals("No"), or even better , with response.equalsIgnoreCase("No") for case insensitive comparison.

Darshan Mehta
  • 27,835
  • 7
  • 53
  • 81
  • Well, changing the while to a conditional statement would not make my code work as intended. That loop is supposed to evaluate all different words within the input sentence. – PN.Perrin Dec 09 '16 at 23:43
  • 1
    That is an infinite loop. It waits for other sentence once it's finished processing the first sentence whereas ideally it should ask whether user wants to try another sentence. I debugged it before posting the answer here. – Darshan Mehta Dec 09 '16 at 23:44
-1

//EVERYTHING UNDER HERE DOES NOT EXECUTE

You have an infinite while loop.

//while the input still contains words
while (scanWord.hasNext()) {

So, you need to break out of that somehow. For example, don't use the while loop, input a whole sentence at once.

//ask user for input
System.out.print("Enter a sentence: ");
String sentence = scanWord.nextLine();

for (String word : sentence.split("\\s+")) {
    // sentence is split on spaces
}

Just be careful using next() and nextLine() together.


The implementation of compareTo returns 0 when objects are equal, and 1 when one object is greater than the other.

Either use == 0 or just use if (response.equals("Yes")) { }.


Why is capitalization important, though? If you don't even care about case, use

if (response.equalsIgnoreCase("yes")) { }

Also, do not close the scanners within the loop. And you only need one.

If you close one scanner, it closes the backing stream, so you actually are closing both.

Community
  • 1
  • 1
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
  • Okay - thank you! However, the problem is not resolved. I did all the changes you advised me, but it still does not compile the lower part under //EVERYTHING UNDER HERE DOES NOT EXECUTE – PN.Perrin Dec 09 '16 at 23:36
  • Wait - doesn't *compile*, or doesn't run? If there's a compilation error, then you should mention what that is – OneCricketeer Dec 09 '16 at 23:37
  • Also, how could you break from the while loop without using break statements? I try to avoid break statements within while loops. – PN.Perrin Dec 09 '16 at 23:38
  • Sorry, I meant run... It does compile, here is an output: – PN.Perrin Dec 09 '16 at 23:38
  • Enter a sentence: Thank you! hankTay ou!yay hello ellohay – PN.Perrin Dec 09 '16 at 23:39
  • Without using a break, you modify the conditional of the loop to be false. – OneCricketeer Dec 09 '16 at 23:39
  • As you can see, the hello was added after (after ou!yay) by myself...and it still runs. – PN.Perrin Dec 09 '16 at 23:39
  • Your first issue is that `scanWord.hasNext()` will continuously "have next" in most cases, so you need to either 1) have a keyword that breaks out of the loop, or read the sentence differently. 2) You aren't really scanning a sentence anyway with your code as `next()` only grabs the next string up the closest whitespace – OneCricketeer Dec 09 '16 at 23:46
  • Do you have any suggestions for that while loop condition? – PN.Perrin Dec 10 '16 at 00:15
  • I already gave my suggestion in my answer - Use `nextLine` to scan an entire sentence, then split the words out of it – OneCricketeer Dec 10 '16 at 00:25