Results 1 to 11 of 11
  1. #1
    jeremyk is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Question StringTokenizer in a Palindrome program

    Alright, this is my first time posting on here... haha, and i'm not 100% on how to exactly ask a question on this.. but anyhow, for anybody that does'nt know, a palindrome is a word that is spelt the same backwards and forwards.. like racecar. so i have this file and i am supposed to check if the words in it are palindromes using the stringtokenizer... so far i thought i had it until it only goes through the loop one time to check if it is a palindrome or not... where should i be putting the palindrome = isPalindrome(tokener.nextToken()); and the line = file.readLine(); so that it goes back through the loop?? ... i think thats what my problem is anyways. :confused:

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Can you post your code so we can see it?

  3. #3
    jeremyk is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    oh boy sorry it looks so bad, is there a way i can make that better?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    To format it, use the code tags. You can get them from one of the icons above the input window. or [ CODE] [ /CODE] without the spaces
    Label the end of long loops for ease of spotting them.
    } // end of while thru input records

    What/where is the problem with your code? Compile, runtime or output?

    Add System.out.println() statements everywhere you need to see values and logic flow.

  5. #5
    jeremyk is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    output, i have it trying to print at one point and all it gives me for the two counters ( counter and counter2 ) is one and one, so that means that it only goes through it twice... so im guessing the problem is with the reading of the next line somewhere rather...

  6. #6
    jeremyk is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    Java Code:
     import java.io.*;                   // Neeeded for IOException.
    import java.util.StringTokenizer;   // Needed for StringTokenizer.
    import javax.swing.JOptionPane;     // Needed for JOptionPane.
    
    public class TokenizerClass
    {
        public static void main(String[] args) throws IOException
        {
            boolean palindrome = false;        // True or false palindrome
            double counter = 0.0;               // How many times true
            double counter2 = 0.0;              // How many times false
            double percentage = 0.0;            // Percent of palindromes
            
            // Get the file to be analyzed.
            FileReader fRead = new FileReader("as1q4.txt");
            BufferedReader file = new BufferedReader(fRead);
            
            // Get the first line of the file.
            String line = file.readLine();
            
            // Tokenize the first line of the String
            StringTokenizer tokener = new StringTokenizer(line);
            
            while (line != null)
            {
                boolean yesno = false;
                yesno = tokener.hasMoreTokens();
                if (yesno == true)
                {
                    JOptionPane.showMessageDialog(null, "has more tokens..");
                    // Uses the token in the string
                    palindrome = isPalindrome(tokener.nextToken());
                }
    
                // If the word is a palindrome...
                if (palindrome == true )
                {
                    // Adds one to the accumulator for the
                    // calculation of percentage.
                    counter ++;
                    break;
                }
                // if the word is not a palindrome...
                else
                {
                    // Adds one to the accumulator for the
                    // calculation of percentage.
                    counter2++;
                }
                  line = file.readLine();
    
            }// end of while
            // Close the file
            file.close();
            
            JOptionPane.showMessageDialog(null, counter + " " + counter2);
            // Calculate the percentage of palindromes in the file. 
            percentage = counter / (counter + counter2) * 100;
            JOptionPane.showMessageDialog(null, "The words are " +
                                    percentage + "% palindromes");
        }
        
         /**
         * The isPalindrome method returns a true or false value of
         * if the word evaluated was a palindrome or not.
         * @param inputWord the word to be evaluated. 
         * @return If the word is true or false. 
         */
        
        public static boolean isPalindrome(String inputWord)
        {
            boolean pal = true;         // If palindrome is true or false
            
            // Check if the word is a palindrome.
            for (int i = 0; i < inputWord.length() / 2; i++)
            {
                // If word is not palindrome change "pal" to false.
                if (!(inputWord.charAt(i) ==   
                           inputWord.charAt(inputWord.length() - i - 1)))
                {
                   pal = false;
                   break;
                }
                if (i == inputWord.length() / 2 - 1)
                {
                    return true;
                }
            }return false;
        }
    }
    that better?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hello caseyb22,
    New To Java - Help with String Tokenizer! Urgent!

    This current post is presented much better than the one on the forums.sun.com.

    Good luck

  8. #8
    jeremyk is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    owwwch. haha.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I agree with Norm that you should use the poor-man's debugger throughout your code (lots of System.out.println(...) statements). Also consider breaking this code into two smaller programs, one that reads the file, and one that checks strings to see if they are palindromes. debug the two programs separately, and then when both are working, combine them. It's the divide and conquer technique.

  10. #10
    jeremyk is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    Okay thank you very much!

  11. #11
    DouglasDon is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default

    The palindrome program I ran into was something like this:
    import java.io.*;

    public class Palindrome
    {
    public static void main (String[] args)
    throws IOException
    {
    String line;
    StringBuilder letterSequence, reverseLetterSequence;
    BufferedReader in = new BufferedReader(new
    InputStreamReader(System.in));

    while ((line = in.readLine()) != null)
    {
    // ignore case and non-letter characters when checking
    palindromes
    line.toUpperCase();
    letterSequence = lettersInCharSequence(line);
    reverseLetterSequence = new StringBuilder(letterSequence);
    reverseLetterSequence.reverse();

    // after each line of input
    // print "true" if the line is a palindrome, "false" otherwise
    System.out.println(
    letterSequence.length() > 0 // no 0-length
    palindromes!
    &&
    (letterSequence.toString()).equals(reverseLetterSe quence.toString()));
    }
    }

    private static StringBuilder lettersInCharSequence(CharSequence input)
    {
    StringBuilder letters = new StringBuilder();

    for (int i = 0; i < input.length(); ++i)
    if (Character.isLetter(input.charAt(i)))
    letters.append(input.charAt(i));

    return letters;
    }
    }

Similar Threads

  1. StringTokenizer
    By carderne in forum New To Java
    Replies: 1
    Last Post: 01-26-2008, 08:19 PM
  2. How to use StringTokenizer for multiple tokens
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-29-2007, 09:38 AM
  3. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:48 AM
  4. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 PM
  5. Palindrome Test
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:36 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
  •