Java Palindrome Program (am I on track)?


I have only 6 months of Java experience (and I'm also new here) so please bear with me if things don't look entirely right in my code. Please note that it's still a work in progress. I'm trying to write a program that takes in strings and prints only the ones that are palindromes.

I'm supposed to: - create a method named <i>isPalindrome</i>, which has a String parameter and - returns a Boolean based on whether the string is a palindrome or not. Then - modify the main method to use <i>isPalindrome</i> to print only the palindromes.

For example, if I type: "madam James apple mom timer", it should print "madam" and "mom".

This is basically the program I am trying to write: Ex: Let's use the word "madam". The program will check if the first and last letters match ("<i>m</i>ada<i>m</i>"). If that is true, then it'll check the next letters, this time "a" and "a" ("m<i>a</i>d<i>a</i>m). And so on and so forth.

This is the Java code I have so far:

public class Palindrome { private String theWord; //Error: The value of the field Palindrome.theWord is not used public boolean isPalindrome( String theWord ) { int firstPointer = 0; int secondPointer = theWord.length() - 1; for ( int i = 0; i < theWord.length( ); i++ ) { if ( theWord.charAt[0] == theWord.charAt (theWord.length() - 1) ) { //Error: charAt cannot be resolved or is not a field return true; } return false; } } public static void main( String[] theWord ) { Palindrome = new Palindrome( ); //Error: Palindrome cannot be resolved to a variable for ( int i = 0; i < theWord.length; i++ ) { while (firstPointer < secondPointer) { //Error: "firstPointer" cannot be resolved to a variable. "secondPointer" cannot be resolved to a variable if ( theWord.charAt[0] == theWord.charAt (theWord.length() - 1) ) { //Error: charAt cannot be resolved to a variable or is not a field. Cannot invoke length() on the array type String[] firstPointer++; //Error: "firstPointer" cannot be resolved to a variable secondPointer++; //Error: "secondPointer" cannot be resolved to a variable } System.out.println(theWord); } } } }

Any bit of help knowing where I've gone wrong would be greatly appreciated. Please don't just give me the right code. I would like to figure this out. Thank you very much.

**EDIT: I've included the errors as comments in the code now. I'm using Eclipse by the way.

<hr />

-->**EDIT 2: Okay guys. I've read most of your answers and have been able to correct most of my code so far (Thank you all so much so far). The only part I'm still having an issue with right now is this part:

if ( theWord.charAt(i) == theWord.charAt (theWord.length() - i - 1) ) { leftPointer++; rightPointer--;

I'm now getting a <i>"Cannot invoke charAt(int) on the array type String[]"</i> and <i>"Cannot invoke length() on the array type String[]"</i>. Those are the only two errors remaining, then I'll test the code out. I've been trying to resolve them for a while now but I'm still not entirely sure what those errors mean.

Eclipse is suggesting that I change <b>theWord.charAt(i)</b> to <b>theWord.length</b> which is not what I want. It is also suggesting I remove "( )" from <i>length</i> but I don't think that's right either.


Looking at your isPalindrome method :

if ( theWord.charAt(0) == theWord.charAt (theWord.length() - 1)

here you always compare the first character to the last character. In each iteration you should compare a different pair of characters, until you find a pair that doesn't match, or reach the middle of the word.

You should use the i variable of your loop :

if ( theWord.charAt(i) == theWord.charAt (theWord.length() - i - 1)

And the return value should be the exact opposite. If you find a pair of characters that don't match, you return false. Only if the loop ends without returning false, you return true.


Okay, let's break everything down into little sizable chunks.

<ol><li>Input a string</li> <li>Parse the string, check if it is a palindrome.</li> <li>

Print out the words in the string which were palindromes.

public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a sentence: "); String sentence = scan.nextLine(); // 1. String[] words = sentence.split(" "); for (String word : words) { // 3. if (isPalindrome(word)) { System.out.println(word); } } } /** * Check if the string is a palindrome. * @param string * @return True if string is palindrome. */ public static boolean isPalindrome(String string) { // 2. for (int i = 0; i < string.length() / 2; i++) { if (string.charAt(i) != string.charAt(string.length() - i - 1)) { return false; } } return true; }} </li> </ol><h3>Some explanation</h3>

The method/function <em>isPalindrome</em> is static because we are calling it from a static context, that is the main function. If you want to use it non-statically you would place it in a class and create a object from that class. The rest should be understandable. :-)


A better isPalindrome method

The shortest way is probably just <strong>following the definition</strong>: If you reverse the string, and it's still the same, then it's a palindrome:

public static boolean isPalindrome(String input) { String reverse = new StringBuilder(input).reverse().toString(); return input.equalsIgnoreCase(reverse); }

But if there is <strong>an educational goal (?)</strong>, and iterators should be used for some reason then, imho it makes more sense to iterate from the outside towards the inside of the string.

public static boolean isPalindrome(String input) { int length = input.length(); for (int i = 0; i < length/2 ; i++) { if (input.charAt(i) != (input.charAt(length-1-i))) return false; } return true; }

Phrase parsing

In your example you used the input of the main String[] parameter. Here is just some information in case you wanted to split it to words manually.

Equivalent to what you got now:

String[] words = phrase.split("\\s+"); for (String word : words) { // do stuff }

The split method uses a delimiter to split a String into a String[]. The delimiter \\s is a regex that represents all kinds of whitespace (not only spaces, but also tabs, new-line characters, etc ...).

But it's not perfect (and neither is your way), <strong>there can still be commas</strong>, dots and other marks in the phrase. You could filter these characters in an iteration, using the Character.isLetterOrDigit method. Alternatively, you could just perform a replace(...) to remove comma's, points and other marks. Or you could use more complex regular expressions as well.

About your code

The first error message : <strong>"The value of the field is not used"</strong>. The error message is caused by the global private field theWord, because it is never used. It's not used because you also have a parameter with the same name inside the method isPalindrom(String theWord). Whenever you reference theWord inside that method, it will always give advantage to method arguments before considering global variables.

It looks like you are stuck here with <strong>a design contradiction</strong>. What exactly is the class Palindrome ? There are 2 options:

<ol><li>Is it supposed to be a toolbox like the Math class ? like boolean value = Palindrome.isPalindrome("madam");?</li> <li>Or is it supposed to be an Object that you instantiate using a constructor ? like boolean value = new Palindrome("madam").isPalindrome();</li> </ol>

Option 1: a toolbox:

public class Palindrome { // removed the private field theWord // make this method static !! public static boolean isPalindrome( String theWord ) { ... } public static void main( String[] theWord ) { // remove the Palindrome object // inside the loop check use the static method // which does not require an object. if ( Palindrome.isPalindrome(word)) { } } }

Option 2: an object

public class Palindrome { // keep the private field theWord private String theWord; public Palindrome(String theWord) { // set the value of the argument to the private field this.theWord = theWord; } // don't make this method static // also you don't need the parameter any more. // it will now use the global field theWord instead of a parameter. public boolean isPalindrome() { ... } public static void main( String[] theWord ) { // inside the loop check use an object Palindrome palindrome = new Palindrome(word); if ( palindrome.isPalindrome()) { } }

As for the errors about the firstPointer and secondPointer. You need to define and initialize those variables. I.e. put int firstPointer = 0; before the loop.


In the loop check it out this way:

boolean isPalin = true; for ( int i = 0; i < theWord.length( )/2; i++ ) { // loop goes till half since no need to check after that if ( !(theWord.charAt(i) == theWord.charAt (theWord.length() - 1 - i)) ) { // will check each letter with each end letter isPalin = false; break; } } return isPalin;

Another things to add -

1 -<strong>firstPointer secondPointer</strong> are local variables to isPalindrome

2 - When u have decalared theWord as global variable there doent seems a need to pass it. You can use it within the same class.

3 - theWord in main(String[] theWord) would require you to provide input as arguments, it better you go for console input at runtime.

4 - In main you should split each word and pass it to isPalindrome. In your code you are not calling isPalindrome to check anywhere.


  • Qt Connect signals with different arguments
  • Why are my web pages zoomed in when I open them in Opera Mobile?
  • How can I encode a filename according to RFC 2231?
  • Adding independent aspx/asmx pages into DotNetNuke
  • Single django queryset to get n adjacent items
  • xtable - background colour of added rows
  • cell spacing in div table
  • XSLT foreach repeating nodes to flat
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)
  • how to set variables in a php include file?
  • Find group of records that match multiple values
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Center align outputs in ipython notebook
  • android google indoor map
  • How to revert to previous XCode version?
  • Eloquent paginate function in Slim 3 project using twig
  • How to define and use opencv mat of user type
  • Custom Tabgroup Appcelerator
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Extracting HTML between tags
  • Linq Objects Group By & Sum
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Projection media query: browser support and workarounds?
  • Different response to non-authenticated users and AJAX calls
  • Optimizing database types to compact database (SQLite)
  • Fetching methods from BroadcastReceiver to update UI
  • Where to put my custom functions in Wordpress?
  • vba code to select only visible cells in specific column except heading
  • Symfony2: How to get request parameter
  • GridView Sorting works once only
  • Numpy divide by zero. Why?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Buffer size for converting unsigned long to string
  • SQL merge duplicate rows and join values that are different
  • WPF Applying a trigger on binding failure
  • -fvisibility=hidden not passed by compiler for Debug builds
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • reshape alternating columns in less time and using less memory