0

i have an assignment where i have to make a program that reads input and evaluates whether or not it is a palindrome until the user terminates. i am having issues with the while loop. it works like it should upon first input, but then when it re-enters the loop, it always says it is not a palindrome, even when it is.

Scanner in;
    in = new Scanner (System.in);

    String original = "";       //variable for user input
    String original2 = "";      //variable to help make input case insensitive
    String reverse = "";        //variable to compare word in reverse
    int length;                 //variable used to 
    int i;                      //variable used in for loop
    //String response = "";

    System.out.println("Enter a word to check if it is a palindrome!");

    original = in.nextLine();

    while (!original.equals("n")) 
    {
        length = original.length();

        original2 = original;       //assigning a new variable to hold the input, so as to not change it

        original2 = original.toLowerCase(); //converting input to all lower case using the holder variable 

        for (i = length -1; i >= 0; i--)    //for loop reversing the word to check for palindrome status
        {
            reverse = reverse + original2.charAt(i);
        }

        if (original2.equals(reverse))      //if statement determining output if palindrome is true or not
        {
            System.out.println(original + " is a palindrome");
        }
        else
        {
            System.out.println(original + " is not a palindrome");
        }

        System.out.println("Enter another word or enter 'n' to end");   //allowing to continue
        original = in.nextLine();

    }

System.out.println("thank you!");
    in.close();
}

i initially thought that it was an issue with the buffer, but i no longer think that is an issue.

i will really appreciate any help at all

  • 2
    `original != "n"` is testing if you have exactly the same string object (== and != in Java test for object identity). You want `!original.equals("n")`; the `equals` method checks if two objects have the same value. – Alan Stokes Jul 13 '14 at 16:01
  • 1
    [Possibly related question](http://stackoverflow.com/q/5032356/440558). – Some programmer dude Jul 13 '14 at 16:01

1 Answers1

1

You don't reset reverse, so the reversed string is constantly being built on

Kevin L
  • 1,036
  • 3
  • 12
  • 21
  • how do i reset reverse? please forgive my significant lack of knowledge/experience... – tylerandrewandersen Jul 13 '14 at 23:57
  • @tylerandrewandersen `reverse = "";` (if you put the word `aaa` in, reverse = `aaa` after the first loop. if you then put in `bbb`, you start adding letters to `aaa` because reverse still = `aaa`, so you get stuff like `aaabbb`) – Kevin L Jul 14 '14 at 13:02
  • yes, thank you. i feel like an idiot for not figuring that out on my own...i needed to initialize the variable at the beginning of the while loop. it works perfectly now, thank you – tylerandrewandersen Jul 14 '14 at 15:49