Hey everyone this is my first post. I am working on this program to find all palindromes depending on what someone types. I have it so it displays if the word they type is a palindrome first and then I'm supposed to display any palindromes anywhere in that text that is submitted. I couldn't think of an easier way to do this so I tried to use StringTokenizer. I wanted to loop through the ascii table and change the delimiter of the StringTokenizer with every loop however I'm getting an error stating: "cannot find symbol" I am also supposed to display the number of palindromes inside the text. For example if I type: "ABBA" I should get the text saying it's a palindrome and then if i find all palindromes inside it it should display 2. Here is my code:
Code:import java.util.*;
public class Main {
public static void printText(String txtIn)
{
System.out.println("The text to print is: " + txtIn);
}
public static boolean palindrome(String txtIn)
{
for (int i=0;i<(txtIn.length()/2);i++)
{
if (txtIn.charAt(i) != txtIn.charAt(txtIn.length() -1 -i))
{
return false;
}
if (txtIn.length() <= 1)
{
return false;
}
}
return true;
}
public static boolean findAllPalindromes(String txtIn)
{
int count=0;
for (int i=0; i<128; i++)
{
char delim = (char)i;
StringTokenizer st = new StringTokenizer(txtIn, delim);
while (st.hasMoreTokens())
{
String x = st.nextToken();
if (x.length() <= 1)
{
continue;
}
else
{
palindrome(x);
if (palindrome(x) == true)
{
count = count + 1;
}
}
}
}
System.out.println(count);
return false;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a string of text: ");
String input = in.nextLine();
//Print text
printText(input);
//Checks to see if it's a palindrome
if (palindrome(input))
{
printText("The string is a palindrome.");
}
else
{
printText("The string is not a palindrome.");
}
//Find all palindromes
System.out.print("Would you like to find all Palindromes in this text? ");
String findPalin = in.nextLine();
while (!findPalin.equals("yes") && !findPalin.equals("no"))
{
System.out.print("Please answer yes or no: ");
String findPalin1 = in.nextLine();
findPalin = findPalin1;
if (findPalin.equals("yes"))
{
break;
}
if (findPalin.equals("no"))
{
break;
}
}
if (findPalin.equals("yes"))
{
findAllPalindromes(input);
}
if (findPalin.equals("no"))
{
System.out.println("Okay. We'll maybe next time then.");
}
}
}

