Results 1 to 7 of 7
  1. #1
    spleenlol is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default 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.");
            }
    
        }
    
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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)

  3. #3
    spleenlol is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    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)
    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
    It's this line:
    Java Code:
     StringTokenizer st = new StringTokenizer(txtIn, delim);
    delim has a red squiggly under it and it says: "cannot find symbol"

  4. #4
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    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;
    	}
    }

  5. #5
    spleenlol is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    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);
                    }
                }
            }
    I know the first loop runs the length that the string is but what does the inside loop do? I see it increments it by two but I don't quite understand how that is checking if it is a palindrome or not. Sorry :confused:

  6. #6
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    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.

  7. #7
    spleenlol is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    ohhhh! Thanks so much Turtle. I understand what that does now! Thank so much :D

Similar Threads

  1. how to find a datatype
    By hasysf in forum New To Java
    Replies: 1
    Last Post: 09-06-2009, 10:41 AM
  2. [SOLVED] couldn't find a way
    By tOpach in forum New To Java
    Replies: 2
    Last Post: 12-17-2008, 12:00 AM
  3. Can't find class
    By bozovilla in forum New To Java
    Replies: 2
    Last Post: 07-31-2008, 05:45 AM
  4. My JSP can't find the servlet
    By gabriel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-29-2007, 09:01 PM
  5. cannot find a JVM installed
    By tommy in forum New To Java
    Replies: 2
    Last Post: 07-29-2007, 08:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •