5 Replies - 579 Views - Last Post: 29 October 2012 - 07:50 PM Rate Topic: -----

#1 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Java Program to Identify Palindromes

Posted 29 October 2012 - 06:57 PM

Hi everyone! I amy trying to write a program to identify whether a string that has been inputted by the user is a palindrome or not but my output is wrong /: Here is my code! If somebody could help me with what's wrong it would be much appreciated (:

import java.util.Scanner;
public class Palindrome {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Please enter a string: ");
		String s = input.next();
		char [] palindrome = s.toCharArray();
		
		if (isPalindrome(palindrome)) {
			System.out.println(s + " is a palindrome.");
		} else {
			System.out.println(s + " is not a palindrome. ");
		}
	}
	
	public static boolean isPalindrome(char[] palindrome) {
		char[] result = new char[palindrome.length];
		for (int i = 0, j = palindrome.length - 1; i < palindrome.length; i++, j--) {
			result[j] = palindrome[i];
		}
		if (palindrome == result) {
			return true;
		}
		return false;
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Program to Identify Palindromes

#2 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Program to Identify Palindromes

Posted 29 October 2012 - 07:18 PM

You can't compare arrays like that
if (palindrome == result) {

to test if a String is a plaindrome just compare if

digit[0] == digit[digit.length-1]
digit[1] == digit[digit.length-2]
digit[2] == digit[digit.length-3]
up to
digit[digit.length / 2]
Was This Post Helpful? 0
  • +
  • -

#3 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Java Program to Identify Palindromes

Posted 29 October 2012 - 07:24 PM

The problem is you are not comparing the values of the two arrays. Let's take a look at an example.

char[] set1 = {'a', 'b', 'c'};
char[] set2 = {'a', 'b', 'c'};
		
System.out.println(set1 == set2); //prints false



When you compare one array to another using == it does not compare the values in the array.

EDIT: Darn you pbl you beat me to it. >.<

This post has been edited by Kinaces: 29 October 2012 - 07:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Java Program to Identify Palindromes

Posted 29 October 2012 - 07:42 PM

but pbl, how will that work? Because how will you account for the difference in the lengths of the strings the the user will enter?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Program to Identify Palindromes

Posted 29 October 2012 - 07:49 PM

That should do it
boolean isPalindrome(char[] digit) {
   int last = digit.length - 1;

   for(int i = 0; i < digit.length/2; ++i) {
       if(digit[i] != digit[last])
           return false;
       --last;
   }

   return true;
}


Was This Post Helpful? 1
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 848
  • View blog
  • Posts: 2,585
  • Joined: 29-July 11

Re: Java Program to Identify Palindromes

Posted 29 October 2012 - 07:50 PM

You're being way too complicated. If you just want to check only single words like mom, dad, pop, racecar, etc, you can just do:

public boolean isPalindrome(String s) {
        String input = s.toLowerCase();
        String reversed = new StringBuffer(input).reverse().toString();
    
        return input.equals(reversed);
}



If you want to check for phrases and sentences, it becomes a little bit more complicated, because you have to deal with spacing, but all you need to do is build a one string from input with no spaces (String.split()), then another with no spaces reversed, then check their equality.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1