I am trying to receive a string, cut off the first letter, place it at the end of the string, and compare it to the original input. For some words it works and others it does not. I am not sure if this is a problem with the loop or what?
import java.util.*;
public class Palin{
public static void main(String args[])
{
String original,input;
Scanner sc=new Scanner(System.in);
System.out.println("Enter word and I will tell you if it has the property of a palindrome: ");
original = sc.nextLine();
char firstLetter = original.charAt(0);
input = original.substring(1);
input = input + firstLetter;
StringBuilder input2=new StringBuilder(input).reverse();
String s2=new String(input2);
do{
if(original.equals(s2))
System.out.println(original + " is a Palindrome");
else
System.out.println(original + " is not a Palindrome");
System.out.println("Enter another word. Or enter \"quit\" to end");
original = sc.nextLine();
} while (!(original.equalsIgnoreCase("quit")));
}
}
I think I just had to cleanup references and location of the do-while. Thanks for the help.!
Output
Enter word and I will tell you if it has the property of a palindrome: Palindrome Palindrome does not have a Palindrome property Enter another word. Or enter "quit" to end uneven uneven has a Palindrome property Enter another word. Or enter "quit" to end banana banana has a Palindrome property Enter another word. Or enter "quit" to end dresser dresser has a Palindrome property Enter another word. Or enter "quit" to end quit
Revised code below.
import java.util.*; public class Palin{
public static void main(String args[])
{
String original,input;
Scanner sc=new Scanner(System.in);
System.out.println("Enter word and I will tell you if it has the property of a palindrome: ");
original = sc.nextLine();
do{
char firstLetter = original.charAt(0);
input = original.substring(1);
input = input + firstLetter;
StringBuilder input2=new StringBuilder(input).reverse();
String s2=new String(input2);
if(original.equals(s2))
System.out.println(original + " has a Palindrome property");
else
System.out.println(original + " does not have a Palindrome property");
System.out.println("Enter another word. Or enter \"quit\" to end");
original = sc.nextLine();
} while (!(original.equalsIgnoreCase("quit")));
}
}