Note that there are some explanatory texts on larger screens.

plurals
  1. POtesting palindromes with nested loops ONLY
    text
    copied!<p>I need to write a program that gets a string from the user and tests to see if it's a palindrome. This has to be done with nested loops: I can't write a method to return the answer. I also have to keep accepting string inputs and testing them until the user enters an empty line, at which point the program prints 'goodbye' and terminates. What I'm having trouble with is getting the program to accept input after the two possible points of input (is a palindrome, is not), and to then use the new input in the loops, and print the appropriate line each time.</p> <p>Here's what the output is supposed to look like:</p> <p>Enter a string: rotor</p> <p>rotor is a palindrome.</p> <p>Enter a string: mummy</p> <p>mummy is not a palindrome.</p> <p>Enter a string:</p> <p>Empty line read. Goodbye.</p> <p>This is what I have so far, and it tests each input and returns the correct statement, but it does nothing when the input is empty:</p> <pre><code>System.out.print("Enter a string: "); String input = in.next(); if (input.length() &gt; 0) { int x = 0; int y = input.length()-1; while (x &lt; y) { if (input.charAt(x) == input.charAt(y)) { x++; y--; } else { System.out.println(input + " is NOT a palindrome."); System.out.println("Enter a string: "); input = in.next(); } System.out.println(input + " is a palindrome."); System.out.println("Enter a string: "); input = in.next(); } } else { System.out.print("Empty line read - Goodbye!"); } </code></pre> <p>Any thoughts? This is homework, btw, so I'm not looking for The Answer so much as clues, or what I need to be looking at.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload