Palindrome with recursion

somethings wrong with my main

Page 1 of 1

13 Replies - 15368 Views - Last Post: 23 July 2008 - 07:44 PM Rate Topic: -----

#1 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Palindrome with recursion

Post icon  Posted 23 July 2008 - 11:15 AM

I am having trouble with this assignment. I think i have the recursion code right my problem is with the main..Am I getting the control to come back to the main?...........any direction would be a blessing.
d




import java.util.*;
import java.lang.*;
public class Palindrome
 {
 	static Scanner console = new Scanner(System.in);
 
	public Palindrome()
		 { 
		  
	 } 
	
	public static void main(String[] args) 
		{//declare the variables
			String str;
			
			//prompt the user
			System.out.print("Enter a word to see if its a Palindrome; ");
			str= console.next();
			System.out.println();
			//
			//the answer
			if(isPalindrome()= true)
				
					System.out.println( str + "yes is a palindrome");
				else
					System.out.println(str + "no not a palindrome");	
	
		
	}

	 public static boolean isPalindrome(String str)
	 {
	 	return isPalindrome(str,0,str.length()-1);
	 	
	 }
   public static boolean isPalindrome(String str,int low, int high)
   {  if(high <= low)
   		   return true;
   		  else if (str.charAt(low)!= str.charAt(high))
   			  return false;
   			  else
   				  return isPalindrome(str,low+1,high-1);
   	
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Palindrome with recursion

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5943
  • View blog
  • Posts: 12,871
  • Joined: 16-October 07

Re: Palindrome with recursion

Posted 23 July 2008 - 12:26 PM

The code is fine. In runs, gives an answer, and completes. What exactly do you want it to do?
Was This Post Helpful? 0
  • +
  • -

#3 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: Palindrome with recursion

Posted 23 July 2008 - 12:35 PM

View Postbaavgai, on 23 Jul, 2008 - 12:26 PM, said:

The code is fine. In runs, gives an answer, and completes. What exactly do you want it to do?


i dont get an answer just process completed...its not working
d
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5943
  • View blog
  • Posts: 12,871
  • Joined: 16-October 07

Re: Palindrome with recursion

Posted 23 July 2008 - 12:51 PM

Oops, missed this:
if(isPalindrome()= true)


Which is actually wrong in two distinct ways; no parameter value and attempted assignment.

Make it this and see how far you get:
if(isPalindrome(str))

Was This Post Helpful? 0
  • +
  • -

#5 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: Palindrome with recursion

Posted 23 July 2008 - 02:57 PM

View Postbaavgai, on 23 Jul, 2008 - 12:51 PM, said:

Oops, missed this:
if(isPalindrome()= true)


Which is actually wrong in two distinct ways; no parameter value and attempted assignment.

Make it this and see how far you get:
if(isPalindrome(str))


OH THANK YOU !!!!
I almost had it......

Can i ask one more thing. How do i set up a loop to prompt user ?
I get an endless loop how do i stop it?

d

String str,another="y";
			
			//prompt the user
//is this how do do loop
while(another.equalsIngnoreCase("y"))
			System.out.print("Enter a word to see if its a Palindrome; ");
			str= console.next();
			System.out.println();
			//
			//the answer
			
			if(isPalindrome(str))
				
					System.out.println( str + " is a palindrome");
				else
					System.out.println(str + " not a palindrome");	
	
			 System.out.print("test another(y/n)?");
			
			
			 another= console.next();
			 
		
	}

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Palindrome with recursion

Posted 23 July 2008 - 04:36 PM

Your

while(another.equalsIgnoreCase("y"))

loop works for me.. if you enter anything else than "y" or "Y" when prompted it exits
Was This Post Helpful? 0
  • +
  • -

#7 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: Palindrome with recursion

Posted 23 July 2008 - 06:26 PM

View Postpbl, on 23 Jul, 2008 - 04:36 PM, said:

Your

while(another.equalsIgnoreCase("y"))

loop works for me.. if you enter anything else than "y" or "Y" when prompted it exits



No its saying process complete..Is my placement right?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Palindrome with recursion

Posted 23 July 2008 - 06:36 PM

View Postdebeth, on 23 Jul, 2008 - 06:26 PM, said:

View Postpbl, on 23 Jul, 2008 - 04:36 PM, said:

Your

while(another.equalsIgnoreCase("y"))

loop works for me.. if you enter anything else than "y" or "Y" when prompted it exits



No its saying process complete..Is my placement right?


No a { missing after the while

while(another.equalsIgnoreCase("y")) {

or

while(another.equalsIgnoreCase("y"))
{

and a closing one } after

another = console.next();
Was This Post Helpful? 0
  • +
  • -

#9 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: Palindrome with recursion

Posted 23 July 2008 - 06:48 PM

View Postpbl, on 23 Jul, 2008 - 06:36 PM, said:

View Postdebeth, on 23 Jul, 2008 - 06:26 PM, said:

View Postpbl, on 23 Jul, 2008 - 04:36 PM, said:

Your

while(another.equalsIgnoreCase("y"))

loop works for me.. if you enter anything else than "y" or "Y" when prompted it exits



No its saying process complete..Is my placement right?


No a { missing after the while

while(another.equalsIgnoreCase("y")) {

or

while(another.equalsIgnoreCase("y"))
{

and a closing one } after

another = console.next();


Still not working...
see
public static void main(String[] args) 
		{//declare the variables
			String str,another="y";
			//start the loop
			while(another.equalsIngnoreCase("y"))
			{
			

			//prompt the user
			System.out.print("Enter a word to see if its a Palindrome; ");
			str= console.next();
			System.out.println();
			//
			//the answer
			
			if(isPalindrome(str))
				
					System.out.println( str + " is a palindrome");
				else
					System.out.println(str + " not a palindrome");	
			 
			  
			 System.out.print("test another(y/n)? ");
			 another= console.next();
			 
			}
	}


still says process complete. ??
Was This Post Helpful? 0
  • +
  • -

#10 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: Palindrome with recursion

Posted 23 July 2008 - 06:58 PM

my build output says this:
H:\Java2\program2\Palindrome.java:23: cannot find symbol
symbol : method equalsIngnoreCase(java.lang.String)
location: class java.lang.String
while(another.equalsIngnoreCase("y"))
^
1 error

Process completed.

then it has console output...d
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Palindrome with recursion

Posted 23 July 2008 - 07:09 PM

View Postdebeth, on 23 Jul, 2008 - 06:58 PM, said:

my build output says this:
H:\Java2\program2\Palindrome.java:23: cannot find symbol
symbol : method equalsIngnoreCase(java.lang.String)
location: class java.lang.String
while(another.equalsIngnoreCase("y"))
^
1 error

Process completed.

then it has console output...d

Post all your code

use the tag [* code=java] without the *
it will indent correctly your code so we'll see
Was This Post Helpful? 0
  • +
  • -

#12 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: Palindrome with recursion

Posted 23 July 2008 - 07:22 PM

import java.util.*;
import java.lang.String;
public class Palindrome
 {
 	static Scanner console = new Scanner(System.in);
 
    public Palindrome()
    	 { 
          
     } 
    
    public static void main(String[] args) 
    	{//declare the variables
    		String str,another="y";
    		//start the loop
    		while(another.equalsIngnoreCase("y"))
    		{
    		

    		//prompt the user
    		System.out.print("Enter a word to see if its a Palindrome; ");
    		str= console.next();
    		System.out.println();
    		//
    		//the answer
    		
    		if(isPalindrome(str))
    			
    				System.out.println( str + " is a palindrome");
    			else
    				System.out.println(str + " not a palindrome");	
             
              
             System.out.print("test another(y/n)? ");
             another= console.next();
             
    		}
    }

    public static boolean isPalindrome(String str)
     {
     	return isPalindrome(str,0,str.length()-1);
     	
     }
   public static boolean isPalindrome(String str,int low, int high)
   {  if(high <= low)
   	       return true;
   	      else if (str.charAt(low)!= str.charAt(high))
   	      	return false;
   	      	else
   	      		return isPalindrome(str,low+1,high-1);


 
   }
}



like this....
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Palindrome with recursion

Posted 23 July 2008 - 07:35 PM

equalsIgnoreCase not equalsIngnoreCase .... an extra "n"
Was This Post Helpful? 0
  • +
  • -

#14 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

Re: Palindrome with recursion

Posted 23 July 2008 - 07:44 PM

AHHHH.......thank you so much...i was starting to cry.

You guys are great!!! You know a year ago i didn't know what a flash drive was...now i am starting to get some of this stuff.....d
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1