Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-02-2008, 03:44 AM
Member
 
Join Date: Oct 2008
Posts: 6
Rep Power: 0
jeremyk is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-02-2008, 04:08 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Can you post your code so we can see it?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-02-2008, 04:16 AM
Member
 
Join Date: Oct 2008
Posts: 6
Rep Power: 0
jeremyk is on a distinguished road
Default
oh boy sorry it looks so bad, is there a way i can make that better?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-02-2008, 04:22 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-02-2008, 04:24 AM
Member
 
Join Date: Oct 2008
Posts: 6
Rep Power: 0
jeremyk is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-02-2008, 04:33 AM
Member
 
Join Date: Oct 2008
Posts: 6
Rep Power: 0
jeremyk is on a distinguished road
Default
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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-02-2008, 04:38 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,475
Rep Power: 8
Fubarable is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-02-2008, 04:38 AM
Member
 
Join Date: Oct 2008
Posts: 6
Rep Power: 0
jeremyk is on a distinguished road
Default
owwwch. haha.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-02-2008, 04:44 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,475
Rep Power: 8
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 10-02-2008, 04:46 AM
Member
 
Join Date: Oct 2008
Posts: 6
Rep Power: 0
jeremyk is on a distinguished road
Default
Okay thank you very much!
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 02-13-2010, 07:35 PM
Member
 
Join Date: Feb 2010
Posts: 1
Rep Power: 0
DouglasDon is on a distinguished road
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;
}
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
StringTokenizer carderne New To Java 1 01-26-2008 09:19 PM
How to use StringTokenizer for multiple tokens javaplus New To Java 2 11-29-2007 10:38 AM
StringTokenizer Java Tip Java Tips 0 11-08-2007 09:48 AM
StringTokenizer Java Tip Java Tips 0 11-03-2007 10:24 PM
Palindrome Test Ada New To Java 1 05-26-2007 02:36 PM


All times are GMT +2. The time now is 09:25 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org