Home > DeveloperSection > Forums > Java program looping help! I need to restart the program via a y/n input
marcel ethan
marcel ethan

Total Post:105

Posted on    December-30-2014 12:49 AM

 Java Java  Loop 

 1 Reply(s)
 1204  View(s)
Rate this:
Okay so I made a program to check for palindromes but I need to be able to restart the program by asking if the user wants to try again y/n? But I can't seem to loop it correctly. Help me! P.S sorry for formatting.

import java.util.*;
import java.util.Scanner;

class PalidromeTester
public static void main(String args[])
boolean play = true; 

while(play == true){

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

System.out.println("Please enter a potential palindrome:");
inputString = in.nextLine();

int length  = inputString.length();
int i, begin, end, middle;

begin  = 0;
end    = length - 1;
middle = (begin + end)/2;

for (i = begin; i <= middle; i++) {
  if (inputString.charAt(begin) == inputString.charAt(end)) {
  else {
if (i == middle + 1) {
  System.out.println("That string is a palindrome.");
else {
  System.out.println("Sorry, that string is not a palindrome.");

String playagain = in.nextLine();
if (playagain == "y")
play = true;
play = false;


Takeshi Okada
Takeshi Okada

Total Post:89

Posted on    December-30-2014 1:05 AM

Try this:

if ("y".equalsIgnoreCase(playagain))

Don't use == to compare Strings. All that does is compare reference values. You want equals(): it compares String contents.

Don't want to miss updates? Please click the below button!

Follow MindStick