9 Replies - 2188 Views - Last Post: 02 December 2009 - 05:26 PM Rate Topic: -----

#1 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

java palindrome

Posted 02 December 2009 - 11:15 AM

import java.util.Scanner;
public class Palindromes
{
   
   

	public static boolean isPal(String s)
	{
		if(s.length() == 0 || s.length() == 1)
			return true;
		if(s.charAt(0) == s.charAt(s.length()-1))
			return isPal(s.substring(1, s.length()-1));
		return false;
	}

	public static void main(String[]args)
	{
		boolean quit = true;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a word to test whether it is a palindrome or not. Type quit to end.");
		String x = sc.nextLine();
		while(quit) {
		if(isPal(x))
			System.out.println(x + " is a palindrome");
		if else(x = quit)
			quit = false;
		else
			System.out.println(x + " is not a palindrome");
			
		}
	}
} 


im trying to create a program that Prompts the user to input a word or phrase, Allows the user to continue entering input until they choose to quit, has a recursive method to determine if the word or phrase entered is a palindrome, and prints a message that tells the user whether the word or phrase is a palindrome. i have most of this, but im not sure if i am going about it the right way. i tried to make it so that it would continue over and over again, but im getting an error on my if else statement ')' expected. if anyone has any advice or a solution it is much appreciated. thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: java palindrome

#2 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: java palindrome

Posted 02 December 2009 - 11:18 AM

	while(quit) {
		if(isPal(x))
			System.out.println(x + " is a palindrome");
		if else(x = quit)
			quit = false;
		else
			System.out.println(x + " is not a palindrome");
			
		}


your if else(x= quit)
should be
else if(x== quit)


but x is a string and q is a boolean so you cant compare them. get a boolean when you call is pal so...
while(quit) {
boolean itsPal = isPal(x);
		if(itsPal == true){
			System.out.println(x + " is a palindrome");
quit = false;
}

		else
			System.out.println(x + " is not a palindrome");
			
		}


This post has been edited by ianian112: 02 December 2009 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: java palindrome

Posted 02 December 2009 - 11:27 AM

i tried your advice with my code like so:
import java.util.Scanner;
public class Palindromes
{
   
   

	public static boolean isPal(String s)
	{
		if(s.length() == 0 || s.length() == 1)
			return true;
		if(s.charAt(0) == s.charAt(s.length()-1))
			return isPal(s.substring(1, s.length()-1));
		return false;
	}

	public static void main(String[]args)
	{
		boolean quit = true;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a word to test whether it is a palindrome or not. Type quit to end.");
		String x = sc.nextLine();
		while(quit) {
			boolean itsPal = isPal(x);
			if(itsPal == true){
				System.out.println(x + " is a palindrome");
				quit = false;
			}
			else
			System.out.println(x + " is not a palindrome");
			
		}
	}
}

however a problem occurred. when it prompted me to enter a phrase i entered hello and it just said hello is not a palindrome over and over again...im not quite sure of how to go about fixing this.
Was This Post Helpful? 0
  • +
  • -

#4 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: java palindrome

Posted 02 December 2009 - 11:36 AM

I don't think you can use == with strings.

Try something like...

if (x.equals("quit")) {
// then quit
}
Was This Post Helpful? 0
  • +
  • -

#5 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: java palindrome

Posted 02 December 2009 - 12:14 PM

i have updated my code to this:
import java.util.Scanner;
public class Palindromes
{
   
   

	public static boolean isPal(String s)
	{
		if(s.length() == 0 || s.length() == 1)
			return true;
		if(s.charAt(0) == s.charAt(s.length()-1))
			return isPal(s.substring(1, s.length()-1));
		return false;
	}

	public static void main(String[]args)
	{
		boolean quit = true;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a word to test whether it is a palindrome or not. Type quit to end.");
		String x = sc.nextLine();
		while(quit) {
			boolean itsPal = isPal(x);
			if(itsPal == true){
				System.out.println(x + " is a palindrome");
				quit = false;
			if (x.equals("quit")) {
			quit = false;
			}
			else
			System.out.println(x + " is not a palindrome");
		
		}
			
		}
	}
}

now every time i run i get absolutely nothing. i enter a word and then get no response at all. i seem to be digging myself into a hole. any ideas?
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: java palindrome

Posted 02 December 2009 - 12:55 PM

Ok, here is where you made your mistakes:
while(quit) {
			boolean itsPal = isPal(x);
			if(itsPal == true){
				System.out.println(x + " is a palindrome");
				quit = false; <-- Anything missing? Maybe a "}"?
			if (x.equals("quit")) {
			quit = false;
			}
			else <-- This is ALWAYS true unless the user enters "quit". instead of if(x.equals("quit")) use else if(...)
			System.out.println(x + " is not a palindrome");
		



Fixed:
	while(quit) {
		boolean itsPal = isPal(x);
		if(itsPal == true){
			System.out.println(x + " is a palindrome");
			quit = false;
		}
		else if (x.equals("quit")) {
			quit = false;
		}
		else
			System.out.println(x + " is not a palindrome");
	}


This post has been edited by Dogstopper: 02 December 2009 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: java palindrome

Posted 02 December 2009 - 04:05 PM

i used the code, but im having the same problem. when i enter something it continues to say that "hello is not a palindrome over and over again. in reality i want it to prompt me, i respond, it tell me whether it is a palindrome or not, and then start the process over. i dont know what is wrong with it, but it seems as if it should work like i want it to.
import java.util.Scanner;
public class Palindromes
{
   
   

	public static boolean isPal(String s)
	{
		if(s.length() == 0 || s.length() == 1)
			return true;
		if(s.charAt(0) == s.charAt(s.length()-1))
			return isPal(s.substring(1, s.length()-1));
		return false;
	}

	public static void main(String[]args)
	{
		boolean quit = true;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a word to test whether it is a palindrome or not. Type quit to end.");
		String x = sc.nextLine();
		while(quit) {
		boolean itsPal = isPal(x);
		if(itsPal == true){
			System.out.println(x + " is a palindrome");
			quit = false;
		}
		else if (x.equals("quit")) {
			quit = false;
		}
		else
			System.out.println(x + " is not a palindrome");
		}
	}
}

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: java palindrome

Posted 02 December 2009 - 04:15 PM

Worked fine for me! Copied and pasted the code and this is exactly what I got:
Enter a word to test whether it is a palindrome or not. Type quit to end.
racecar
racecar is a palindrome



Why don't you try clean rebuild and run that new Jar

On second thought, get rid of the loop altogether as it is not needed.
public static void main(String[]args)
	{
		boolean quit = true;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a word to test whether it is a palindrome or not. Type quit to end.");
		String x = sc.nextLine();
		boolean itsPal = isPal(x);
		if(itsPal == true){
			System.out.println(x + " is a palindrome");
		}
		else if (x.equals("quit")) {
			return;
		}
		else
			System.out.println(x + " is not a palindrome");
		
	}


Was This Post Helpful? 0
  • +
  • -

#9 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: java palindrome

Posted 02 December 2009 - 04:31 PM

i have it working now to the extent that it will prompt me, i will enter a word, and it will tell me yes or no. what i am trying to do though is allow this to happen over and over again until i type quit. that is why i originally tried to incorporate the loop.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10822
  • View blog
  • Posts: 40,349
  • Joined: 27-December 08

Re: java palindrome

Posted 02 December 2009 - 05:26 PM

Let's start there then. So you want to get the String input, determine if it is a palindrome and display the results, ask the user to play again, and quit when the user wants to stop testing words. Is this correct? If so, the following logic will do the trick. With a do-loop, it always executes at least once, then evaluates the while statement. If the while statement evaluates to true, the loop occurs again. Otherwise, the loop terminates.
0. Begin
1. boolean repeat <-- false
2. do
3.	 String input <-- get input
4.	 if isPalindrome(input)
5.		 print input + " is palindrome"
6.	 end if
7.	 else
8.		  print input + " is not a palindrome"
9.	end else
10.	 input <-- get input for play again
11.   if input != "quit"
12.	  repeat <-- true
13.   end if
14. while(repeat = true)
15. End


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1