-1

I have found a way to make my program recognize if something is a palindrome or not, but when it is not, the program will not repeat.

I already have the boolean section of this implemented before my provided code.

Originally was set with if statements, wanted to replace with while but something like while (!yespalin(potentialp)) doesn't work either

Scanner scan = new Scanner(System.in);      
String potentialp;
System.out.println("Please enter a potential palindrome: ");
potentialp = scan.nextLine();

while (yespalin(potentialp))
{
    System.out.println("That string is a palindrome");  
}
while (!yespalin(potentialp))
    System.out.println("That string is not a palindrome, please enter another: ");  
    potentialp = scan.nextLine();
}   

I want the program to repeat if not a palindrome

Madplay
  • 1,027
  • 1
  • 13
  • 25

1 Answers1

0

For starters I see you are missing "{" on your second while.

However I do not undetstand exactly why do you need 2 while loops.

If you would read your code line by line, you can understand why that the first loop is the only one who prints the "correct" line. However if you enter a bad palindrome, it will never enter the first loop, therefore won't print the desired sentence.

You want 1 loop which will end if you put the correct value if im not mistaken Which would look something like this:

Scanner scan = new Scanner(System.in);      
String potentialp;
System.out.println("Please enter a potential palindrome: ");
potentialp = scan.nextLine();

while (!yespalin(potentialp)){
    System.out.println("That string is not a palindrome, please enter another: ");  
    potentialp = scan.nextLine();  
}
System.out.println("That string is a palindrome"); 

Hope it was helpful.

Eden Eliel
  • 66
  • 7