-1

So I've created this program which manipulates a word that the user gives as input, by taking the moving the first character to the end of the word, spells the rest of the word backwards, then checks if the original word and the new word are the same. It's designed to run until the user enters the word "quit" as the chosen word. I've managed to get the majority of the code working, except, instead of looping back to ask the user for a new word after its checked the previous one, it just continues running forever. The code I've currently got is below:

package uploadTask9_wordPlay;

import java.util.Scanner;

public class wordPlay {

    public static void main(String[] args) {
        // creates scanner object to get a name from the user input
        Scanner userInput = new Scanner(System.in);

            System.out.println("Enter a name to be tested, or 'quit' to exit 
program: ");
            // stores the user input into string testedName
            String testedName = userInput.nextLine();

            // if the user input is "quit", program exits
            if (testedName.equals("quit")) {
                System.out.println("Thanks for using the program");
                System.exit(0);
            }

            // else, do the rest of the code with the given word
            else {

                while (testedName != "quit") {

            // takes first character from userInput
            char firstLetter = testedName.charAt(0);

            // removes first character from testedName
            StringBuilder removeChar = new StringBuilder(testedName);
            removeChar.deleteCharAt(0);

            // adds first character of testedName to the end of removeChar
            String newString = removeChar.toString() + firstLetter;

            // prints newString backwards
            String reverseWord = new 
StringBuffer(newString).reverse().toString();

            // if statement - far simpler way to carry this task out than loops
            // if statement to check if the words are the same
            if (testedName.equals(reverseWord)) { 
                System.out.println(testedName + " is the same as " + reverseWord); }

            else {  
                System.out.println(testedName + " is not the same as " + reverseWord); }

        }   
    }       
}
}

Does anyone know why the program doesn't loop back to the start each time its checked a word?

vdtango
  • 37
  • 2
  • 8
  • Whilst this probably won't fix the problem as a whole; `testedName != "quit"` is wrong. Use `!testedName.equals("quit")` but I fear an infinite loop as it's in your `else` statement and the value doesn't change there. – achAmháin Nov 17 '17 at 12:22
  • @Hovercraft Full Of Eels - that's not *exactly* the issue per se. It doesn't loop back because of the way the loops are positioned. – achAmháin Nov 17 '17 at 12:25
  • Wow this wasn't even a duplicate of what is was marked as... Yes, one mistake is that you're comparing strings with !=, that doesn't work, the other mistake tho is that you're getting the user input before the loop when you need to get it inside the loop instead. – CodedStingray Nov 17 '17 at 12:29
  • As your question was closed - here is a working solution (https://ideone.com/5IT5OS). Basically, you were incorrectly comparing `Strings`, and your `else` loop was never getting a new word, so therefore it would run infinitely. – achAmháin Nov 17 '17 at 12:32
  • Okay thanks so much, I've changed the != now, still pretty used to loops so thanks for the help. – vdtango Nov 17 '17 at 12:38

1 Answers1

0

Don't compare Strings like this:

testedName != "quit"

Do this:

!testedName.equals("quit")
Stefan
  • 2,395
  • 4
  • 15
  • 32