13 Replies - 2744 Views - Last Post: 18 December 2008 - 12:08 PM Rate Topic: -----

#1 frustrated101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-November 08

Palindrome program

Post icon  Posted 16 December 2008 - 10:14 AM

I have constructed a program that takes input from a user and determines whether or not it is a palindrome. However it only works if everything is lower case and there are no punctuations. Can someone help me insert the code that would pick this up? Thanks

 import java.io.*;

	public class Palindromes
   {
	   public static void main (String[]args)throws IOException
	  {
		 BufferedReader in;
	  
	  
		 in = new BufferedReader(new InputStreamReader(System.in));
	  
		 System.out.println("Enter a word or phrase: ");
		 String s = in.readLine();
	  
		 if(isPalindrome(s))
			System.out.println(s + " IS A PALINDROME");
		 
		 else
			System.out.println(s + " IS NOT A PALINDROME");
	  
	  }
   
   
	   public static boolean isPalindrome(String s)
	  {
		 if (s.length() <= 1)
			return true;	// Base case
		 else
		 {
			if (s.charAt(0) == s.charAt(s.length() - 1))
			   return isPalindrome(s.substring(1, s.length() - 1 ) );
			else
			   return false;
		 }
	  }
  


Is This A Good Question/Topic? 0
  • +

Replies To: Palindrome program

#2 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: Palindrome program

Posted 16 December 2008 - 10:31 AM

I made a program that does exactly what you need a few weeks back, so I think I can lend a hand.

First, I'll tell you about two methods you can use together to do what you need. First is the string method you already use, .charAt(index), and the second is one that is automatically imported, Character.isLetterOrDigit(charValue)

Essentially what you do is "clean" the string, so you would make a for loop that iterates the length of the string, and depositing clean characters into a new string, and doing nothing with the other characters.

for(int cnt = 0;cnt < str.length();cnt++)
{
	if(Character.isLetterOrDigit(str.charAt(cnt))
		cleanStr += str.charAt(cnt);
}



That will remove all the punctuation and spaces, to solve your case problem, what you need to do is use the string method .toLowerCase().

str = str.toLowerCase();

Was This Post Helpful? 0
  • +
  • -

#3 frustrated101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-November 08

Re: Palindrome program

Posted 16 December 2008 - 10:47 AM

I am not completely getting all of this program stuff, where does this go in my code? Within the if statements or outside of it?
Was This Post Helpful? 0
  • +
  • -

#4 Mavirick  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 72
  • Joined: 18-February 08

Re: Palindrome program

Posted 16 December 2008 - 08:29 PM

Because isPalindrome() uses recursion, I would add a string to test in that method right after you get in the in-string.

Thus:
System.out.println("Enter a word or phrase: ");
String s = in.readLine();
String toTest = s;
//add your loops
//and methods to
//"clean up" toTest
//in this general
//area :)

if(isPalindrome(toTest)) // then test to see if String toTest is a palindrome, not s
	System.out.println(s + " IS A PALINDROME");
		 
else
	System.out.println(s + " IS NOT A PALINDROME");



Good luck!

-Mav

This post has been edited by Mavirick: 16 December 2008 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Palindrome program

Posted 17 December 2008 - 05:32 AM

i have a code snippet that checks for palindromes but it uses a while loop and not recursion
http://www.dreaminco...snippet2748.htm
maybe this could help
Was This Post Helpful? 0
  • +
  • -

#6 frustrated101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-November 08

Re: Palindrome program

Posted 17 December 2008 - 07:38 AM

Okay, so I have changed my program, I actually need it to read from a file, which I can't test yet since I can't get it to compile. I put in the lines where suggested but obviously I am missing something. I am actually beyond thinking at this point which is part of the problem...however here it is. I get an error at the cleanStr line asking for a "}". I put them in but it keeps coming up with another error.
:blink:

import java.io.*;

	public class Palindromes
   {
	public static BufferedReader inFile;
	   public static void main (String[]args)throws IOException
	  {
		 
			
		 inFile = new BufferedReader(new FileReader("palindrome.dat"));
   
		 
		 String s = inFile.readLine();
		 s = s.toLowerCase();
		 String toTest = s;
		 int index;

  for(int index = 0;index < s.length();index++)
{
	if(Character.isLetterOrDigit(s.charAt(index))
		  cleanStr += s.charAt(index);	 [color=#FF6666]//error starts here[/color]
  
	}	
		
		 if(isPalindrome(toTest))
			System.out.println(s + " IS A PALINDROME");
		 
		 else
			System.out.println(s + " IS NOT A PALINDROME");
	  
	  }
   
   
	   public static boolean isPalindrome(String s)
	  {
		 if (s.length() <= 1)
			return true;	// Base case
		 else
		 {
			if (s.charAt(0) == s.charAt(s.length() - 1))
			   return isPalindrome(s.substring(1, s.length() - 1 ) );
			else
			   return false;
		 }
	  }
  

Was This Post Helpful? 0
  • +
  • -

#7 Mavirick  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 72
  • Joined: 18-February 08

Re: Palindrome program

Posted 17 December 2008 - 12:57 PM

[color=#FF6666]//error starts here[/color]


Is this you? Or did the website screw up in the html? In any case, if that's in your program, it shouldn't be.

for(int index = 0;index < s.length();index++)
{
	if(Character.isLetterOrDigit(s.charAt(index))
		  cleanStr += s.charAt(index);
  
	}


This is your code to "clean up" the string, but String "cleanStr" is not defined. I was suggesting you use toTest to clean up, so toTest's initialization becomes:
String toTest = "";


and then cleanStr in your for loop is replaced with toTest. Before you call on isPalindrome(), though I'd go ahead and convert toTest to all lower case:
toTest = toTest.toLowerCase();



The call to isPalindrome() should then work correctly.

-Mav
Was This Post Helpful? 0
  • +
  • -

#8 frustrated101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-November 08

Re: Palindrome program

Posted 17 December 2008 - 01:24 PM

thank you, it works. Only one more question if I may. It reads in the file but only the first line, where do I put another s = inFile.readLine() to make it read the rest of the lines?
Was This Post Helpful? 0
  • +
  • -

#9 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 61
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Palindrome program

Posted 17 December 2008 - 01:38 PM

View Postfrustrated101, on 17 Dec, 2008 - 12:24 PM, said:

thank you, it works. Only one more question if I may. It reads in the file but only the first line, where do I put another s = inFile.readLine() to make it read the rest of the lines?



you will have to do a statement like this...

while(file.hasNext())
{
//put your input.nextLine(); here
}



This will read every line of the file until there is none left. Cheers
Was This Post Helpful? 0
  • +
  • -

#10 frustrated101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-November 08

Re: Palindrome program

Posted 17 December 2008 - 04:58 PM

Still having issue getting this to work. Used your suggestions but kept getting errors. I believe I have given up and that says alot for me since it takes alot to get me to that point.
Was This Post Helpful? 0
  • +
  • -

#11 Mavirick  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 72
  • Joined: 18-February 08

Re: Palindrome program

Posted 17 December 2008 - 06:46 PM

Why give up when all you've got left is reading the file?

First, make sure you've imported java.io.*;
Then, ensure your main method throws IOException.

The code to read from a file looks like this:
BufferedReader inFile = new BufferedReader(new FileReader(new File("title.txt")));


where title.txt (or .dat) is a valid file in the same folder as your program.

Then, add all your line reading and palindrome checking like this:
while((String s = inFile.readLine())!=null;)
{
	 String lowCase = s.toLowerCase();
	 String toTest = "";
	 int index;

	 for(int index = 0;index < s.length();index++)
	 {
		  if(Character.isLetterOrDigit(lowCase.charAt(index))
			   toTest += lowCase.charAt(index);
	 }	
	   
	 if(isPalindrome(toTest))
		  System.out.println(s + " IS A PALINDROME");
	 else
		  System.out.println(s + " IS NOT A PALINDROME");
	 }
}



-Mav

This post has been edited by Mavirick: 17 December 2008 - 06:48 PM

Was This Post Helpful? 0
  • +
  • -

#12 frustrated101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-November 08

Re: Palindrome program

Posted 18 December 2008 - 06:37 AM

okay so here is the program now, I get an error at the start of my boolean saying it is an illegal start of an expression.
[code]
import java.io.*;


public class PalindromesToday
{
public static BufferedReader inFile;

public static void main (String[]args)throws IOException
{


inFile = new BufferedReader(new FileReader("palindrome.dat"));
String s = inFile.readLine();

while( s !=null)
{
String lowCase = s.toLowerCase();
String toTest = "";
int index;


for(int index = 0;index < s.length();index++)
{
if(Character.isLetterOrDigit(s.charAt(index)))
toTest= toTest.toLowerCase();

}

if(isPalindrome(toTest))
System.out.println(s + " IS A PALINDROME");

else
System.out.println(s + " IS NOT A PALINDROME");

}


public static boolean isPalindrome(s)
{
if (s.length() <= 1)
return true; // Base case
else
{
if (s.charAt(0) == s.charAt(s.length() - 1))
return isPalindrome(s.substring(1, s.length() - 1 ) );
else
return false;

}

}
}
[code]
Was This Post Helpful? 0
  • +
  • -

#13 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 61
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Palindrome program

Posted 18 December 2008 - 11:22 AM

View Postfrustrated101, on 18 Dec, 2008 - 05:37 AM, said:

okay so here is the program now, I get an error at the start of my boolean saying it is an illegal start of an expression.
[code]
import java.io.*;


public class PalindromesToday
{
public static BufferedReader inFile;

public static void main (String[]args)throws IOException
{


inFile = new BufferedReader(new FileReader("palindrome.dat"));
String s = inFile.readLine();

while( s !=null)
{
String lowCase = s.toLowerCase();
String toTest = "";
int index;


for(int index = 0;index < s.length();index++)
{
if(Character.isLetterOrDigit(s.charAt(index)))
toTest= toTest.toLowerCase();

}

if(isPalindrome(toTest))
System.out.println(s + " IS A PALINDROME");

else
System.out.println(s + " IS NOT A PALINDROME");

}


public static boolean isPalindrome(s)
{
if (s.length() <= 1)
return true; // Base case
else
{
if (s.charAt(0) == s.charAt(s.length() - 1))
return isPalindrome(s.substring(1, s.length() - 1 ) );
else
return false;

}

}
}
[code]



Well you are only reading one line from the Text File... so if you want to read more than one line you will need to put a while(File.hasNext()) statement in there to read all of the lines...
Was This Post Helpful? 0
  • +
  • -

#14 Mavirick  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 72
  • Joined: 18-February 08

Re: Palindrome program

Posted 18 December 2008 - 12:08 PM

Quote

String s = inFile.readLine();

while( s !=null)


Like Mark said, this only defines s once, meaning it only calls for a line from the file once. That's why I had the while condition as a redefinition of s for every loop:
while((s = inFile.readLine())!=null)


Or you could use a .hasNext() like Mark suggested.

-Mav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1