Results 1 to 7 of 7
Thread: Find All Palindromes
- 02-02-2010, 09:49 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 11
- Rep Power
- 0
Find All Palindromes
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:
Java 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."); } } }
- 02-02-2010, 11:14 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
I'm getting an error stating: "cannot find symbol"
What is the exact, entire, error message and which line of your code is it referring to? (The line number is often shown after the file name of the code containing the error)
- 02-03-2010, 01:33 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 11
- Rep Power
- 0
Java Code:Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: constructor StringTokenizer(java.lang.String,char) location: class java.util.StringTokenizer at averittlab3.Main.findAllPalindromes(Main.java:44) at averittlab3.Main.main(Main.java:107) Java Result: 1
Java Code:StringTokenizer st = new StringTokenizer(txtIn, delim);
- 02-03-2010, 02:51 AM #4
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
The constructor StringTokenizer(String, char) doesn't exist.
StringTokenizer (Java 2 Platform SE v1.4.2)
Anyway StringTokenizer is not useful for your program.
Study this code:
Java Code:import java.util.Scanner; class Palin { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a string of text: "); String line = in.nextLine(); boolean isPal = palindrome(line); System.out.println(line + " is " + (isPal ? "" : "not ") + "a palindrome"); String subStr; StringBuffer sb = new StringBuffer(line); int length = sb.length(); for(int i=0; i<length; i++) { for (int j=i+2; j<=length; j++) { // Uncomment this to see how the sub string is generated // System.out.println( "i: " + i + " j: " + j + " " + sb.substring(i, j) ); subStr = sb.substring(i, j); if (palindrome(subStr)) { System.out.println(subStr); } } } } public static boolean palindrome(String txtIn) { int length = txtIn.length(); if (length <= 1) return false; int halfWay = length/2; char[] text = txtIn.toCharArray(); for (int i=0;i<halfWay;i++) if (text[i] != text[length -1 - i]) return false; return true; } }
- 02-03-2010, 02:58 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 11
- Rep Power
- 0
Thanks for that code. I'm a little confused as to what this section of the code does:
Java Code:for (int i=0; i<length; i++) { for (int j=i+2; j<=length; j++) { subStr = sb.substring(i, j); if (palindrome(subStr)) { System.out.println(subStr); } } }
- 02-04-2010, 12:51 AM #6
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
The method isPalindrome is used to check if a string is a palindrome.
This section of code checks for all palindromes with in a string.
For example: noonlight
noon
oo
It does this by creating a sub string of the original string, and it passes this sub string to the isPalindrome method.
The two loops are used to generate this sub string. The outer loop is the start of the sub string. The inner loop is the end of the sub string.
To demonstrate what the sub strings look like, try comment out the if statement so that it prints this substring anyway.
- 02-04-2010, 02:33 AM #7
Member
- Join Date
- Feb 2010
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
how to find a datatype
By hasysf in forum New To JavaReplies: 1Last Post: 09-06-2009, 10:41 AM -
[SOLVED] couldn't find a way
By tOpach in forum New To JavaReplies: 2Last Post: 12-17-2008, 12:00 AM -
Can't find class
By bozovilla in forum New To JavaReplies: 2Last Post: 07-31-2008, 05:45 AM -
My JSP can't find the servlet
By gabriel in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-29-2007, 09:01 PM -
cannot find a JVM installed
By tommy in forum New To JavaReplies: 2Last Post: 07-29-2007, 08:23 PM
Bookmarks