Welcome to Dream.In.Code
Become a Java Expert!

Join 149,499 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,389 people online right now. Registration is fast and FREE... Join Now!




palindrome problem

 
Reply to this topicStart new topic

palindrome problem

javascreams
22 Mar, 2007 - 01:10 AM
Post #1

New D.I.C Head
*

Joined: 22 Mar, 2007
Posts: 3


My Contributions
blink.gif
Everyone, i need your help. Im really confused on a question that contains a palindrome. i need my code to produce an Output of

"7 words in a palindrome.txt"
"palindrome.txt contains a palindrome"

The text file contains "able was i ere i saw elba"...
I would really appricate your help on this. Thanx

I need yous to write out this code for me. Im lost with it.

This post has been edited by javascreams: 22 Mar, 2007 - 04:21 AM
User is offlineProfile CardPM
+Quote Post

horace
RE: Palindrome Problem
22 Mar, 2007 - 01:32 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(javascreams @ 22 Mar, 2007 - 09:10 AM) *

blink.gif
Everyone, i need your help. Im really confused on a question that contains a palindrome. i need my code to produce an Output of

"7 words in a palindrome.txt"
"palindrome.txt contains a palindrome"

The text file contains "able was i ere i saw elba"...
I would really appricate your help on this. Thanx

a simple way is to read the characters into a StringBuffer, make a copy and reverse it, then compare the two.
User is offlineProfile CardPM
+Quote Post

javascreams
RE: Palindrome Problem
22 Mar, 2007 - 04:21 AM
Post #3

New D.I.C Head
*

Joined: 22 Mar, 2007
Posts: 3


My Contributions
QUOTE(horace @ 22 Mar, 2007 - 02:32 AM) *

QUOTE(javascreams @ 22 Mar, 2007 - 09:10 AM) *

blink.gif
Everyone, i need your help. Im really confused on a question that contains a palindrome. i need my code to produce an Output of

"7 words in a palindrome.txt"
"palindrome.txt contains a palindrome"

The text file contains "able was i ere i saw elba"...
I would really appricate your help on this. Thanx

a simple way is to read the characters into a StringBuffer, make a copy and reverse it, then compare the two.



I am totally lost with this. Can you write the code out for me.
User is offlineProfile CardPM
+Quote Post

horace
RE: Palindrome Problem
22 Mar, 2007 - 04:40 AM
Post #4

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(javascreams @ 22 Mar, 2007 - 12:21 PM) *


I am totally lost with this. Can you write the code out for me.

have a look at the example here
http://www.java2s.com/Code/JavaAPI/java.la...fferreverse.htm

User is offlineProfile CardPM
+Quote Post

javascreams
RE: Palindrome Problem
22 Mar, 2007 - 05:20 AM
Post #5

New D.I.C Head
*

Joined: 22 Mar, 2007
Posts: 3


My Contributions
QUOTE(horace @ 22 Mar, 2007 - 05:40 AM) *

QUOTE(javascreams @ 22 Mar, 2007 - 12:21 PM) *


I am totally lost with this. Can you write the code out for me.

have a look at the example here
http://www.java2s.com/Code/JavaAPI/java.la...fferreverse.htm




This is as far as i got. It writes the file backwards. Can any1 finish my code by making the output of it to say

"7 words in a palindrome.txt"
"palindrome.txt contains a palindrome"



import java.util.*;
import java.io.*;

class Assignment2edit
{
public static void main(String args[])
{
try
{

FileReader reader = new FileReader("Palindrome.txt");

Scanner inputFile = new Scanner(reader);
String s = inputFile.nextLine();

StringBuilder sb = new StringBuilder (s);
StringBuilder sbRev = sb.reverse();
System.out.println(sbRev);


reader.close(); // Close textfile.txt
}
catch(Exception e)
{
e.getMessage();
}

}

User is offlineProfile CardPM
+Quote Post

horace
RE: Palindrome Problem
22 Mar, 2007 - 05:54 AM
Post #6

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
you can now use the equals() method to comare the strings

User is offlineProfile CardPM
+Quote Post

theviaster
RE: Palindrome Problem
23 Mar, 2007 - 10:50 AM
Post #7

New D.I.C Head
*

Joined: 9 Jan, 2007
Posts: 3


My Contributions
I would use a Scanner to read all of the input into a String. Then, split the string into substrings, delimited by spaces with a StringTokenizer. Finally, you can iterate the tokens
CODE
while(input.hasMoreTokens())
and use .reverse() and .equals(), which are String methods to compare them.

Hope this helps.
User is offlineProfile CardPM
+Quote Post

horace
RE: Palindrome Problem
25 Mar, 2007 - 05:38 AM
Post #8

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(theviaster @ 23 Mar, 2007 - 06:50 PM) *

I would use a Scanner to read all of the input into a String. Then, split the string into substrings, delimited by spaces with a StringTokenizer. Finally, you can iterate the tokens
CODE
while(input.hasMoreTokens())
and use .reverse() and .equals(), which are String methods to compare them.

Hope this helps.

StringTokenizer is a legacy class
http://java.sun.com/j2se/1.5.0/docs/api/ja...gTokenizer.html

it is now recommended to use the split() method from String
http://java.sun.com/j2se/1.4.2/docs/api/ja....String,%20int)

for more discussion see
http://forum.java.sun.com/thread.jspa?thre...ssageID=4400508

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:23PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month