3 Replies - 8702 Views - Last Post: 03 October 2010 - 04:57 PM Rate Topic: -----

#1 jakec112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-September 10

Java Palindrome Program problem

Posted 03 October 2010 - 04:41 PM

I am having problems writing this code for this program. The point of the program is this: Write a program called Palindrome that allows the user to enter a string and determines whether the string is a palindrome. A palindrome is a word, phrase or any sequences of characters that can be read the same way in either direction regardless of the case after removing spaces and any other characters that are not either letters or digits. The empty string is a palindrome.

Here is my code:

import java.util.Scanner;
public class Palindrome {
public static void main (String [] args){
Scanner keyboard=new Scanner(System.in);

System.out.println("Please enter any sequence of characters:");
String user_input = keyboard.nextLine();

boolean isPalindrome = false;
int left = 0;
int right = user_input.length()-1;

while(left<right&&left!=right){
if(user_input.charAt(left) != user_input.charAt(right)){
isPalindrome = true;
break;
}
else{
isPalindrome = false;
}

left++;
right--;
}

if(isPalindrome == true){
System.out.println(user_input + " " + "is not a palindrome ");
}
else{
System.out.println(user_input + " " + "is a palindrome");

}
}

}



Here are some sample outputs that I should follow:
Please enter any sequence of characters: Able was I ere I saw Elba
″Able was I ere I saw Elba″ is a palindrome.

or

Please enter any sequence of characters: 189876
″189876″ is not a palindrome

Any help would be great!

This post has been edited by Dogstopper: 03 October 2010 - 04:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Palindrome Program problem

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



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

Re: Java Palindrome Program problem

Posted 03 October 2010 - 04:45 PM

In the future, please :code: and format your code nicely. I reformatted it for you here:

import java.util.Scanner;

public class Palindrome {
	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);

		System.out.println("Please enter any sequence of characters:");
		String user_input = keyboard.nextLine();

		boolean isPalindrome = false;
		int left = 0;
		int right = user_input.length() - 1;

		while (left < right && left != right) {
			if (user_input.charAt(left) != user_input.charAt(right)) {
				isPalindrome = true;
				break;
			} else {
				isPalindrome = false;
			}

			left++;
			right--;
		}

		if (isPalindrome == true) {
			System.out.println(user_input + " " + "is not a palindrome ");
		} else {
			System.out.println(user_input + " " + "is a palindrome");

		}
	}

}



Now, you need to remove all punctuation and spaces and then make everything the same case before attempting to find whether its a palindrome. You can use the Character and String methods to do this.

Also, you need to rethink your theme here. Let's assume it's a palindrome until it's proven to not be:
boolean isPalindrome = true;
		int left = 0;
		int right = user_input.length() - 1;

		while (left < right && left != right) {
			if (user_input.charAt(left) != user_input.charAt(right)) {
				isPalindrome = false;
				break;
			}

			left++;
			right--;
		}

		if (isPalindrome == true) {
			System.out.println(user_input + " " + "is a palindrome ");
		} else {
			System.out.println(user_input + " " + "is not a palindrome");

		}



Now, it will give you correct output IF there is no punctuation/spaces.

This post has been edited by Dogstopper: 03 October 2010 - 04:49 PM

Was This Post Helpful? 1
  • +
  • -

#3 jakec112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-September 10

Re: Java Palindrome Program problem

Posted 03 October 2010 - 04:51 PM

View PostDogstopper, on 03 October 2010 - 03:45 PM, said:

In the future, please :code: and format your code nicely. I reformatted it for you here:

import java.util.Scanner;

public class Palindrome {
	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);

		System.out.println("Please enter any sequence of characters:");
		String user_input = keyboard.nextLine();

		boolean isPalindrome = false;
		int left = 0;
		int right = user_input.length() - 1;

		while (left < right && left != right) {
			if (user_input.charAt(left) != user_input.charAt(right)) {
				isPalindrome = true;
				break;
			} else {
				isPalindrome = false;
			}

			left++;
			right--;
		}

		if (isPalindrome == true) {
			System.out.println(user_input + " " + "is not a palindrome ");
		} else {
			System.out.println(user_input + " " + "is a palindrome");

		}
	}

}



Now, you need to remove all punctuation and spaces and then make everything the same case before attempting to find whether its a palindrome. You can use the Character and String methods to do this.

However, was there any specific problem you needed help with?



Okay thanks great I will do that in the future, and actually I guess I should have revised it to say actually that case does not matter anymore. My problem is that I try and enter: Able was I ere I saw Elba. and the program reads it as NOT a palidrome, when it actually is. How do I fix this?
[/quote]
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



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

Re: Java Palindrome Program problem

Posted 03 October 2010 - 04:57 PM

if (user_input.charAt(left) != user_input.charAt(right)) {
				isPalindrome = true;



? Why do you set isPalindrome to true if it IS NOT a palindrome. Re-read my last post. I posted more while you were responding.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1