• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Palindromes

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everybody!
I'm a Java rookie floundering my way through my first Java class and I've smacked into a brick wall. I'm trying to write an application to test for numerical palindromes that are 5 characters long. I have gotten the input and output components together but cannot figure out how to do the testing. I'm trying to use loops or if/then statements to compare the first and 5th characters and 2nd and 4th characters to test for the palindrome but don't know how to divide the string into individual characters for the comparison. I was just curious if anybody had any ideas or if I'm going about this all wrong. Any assistance would be greatly appreciated - thanks in advance.
 
author
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to loop through a String you might want to try a StringReader (though this might be overkill for 5 character palindromes). Here's and example:

Or if you want to read the whole string try this

Of course, you can also use the String.getChars() method to return the same array of chars as example2 above. Look at the java docs for more info on this.
Also, as a point of interest, notice that if you checking a string for a palindrome,
1) if the string is an even # of chars, then split the string down the middle, reverse one of them and then they should be identical.
2) if the string is an odd # of chars, then take (n-1)/2 from the beginning and end, flip one and compare as above.
Soooo....you might notice that if you create two char[] arrays and use the StringBuffer to populate them accordingly...
Fun stuff!
Sean

[This message has been edited by Sean MacLean (edited November 09, 2000).]
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this, using Sean's idea about flipping half the string?

[This message has been edited by Graeme Brown (edited November 10, 2000).]
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, You say that it is numerical. Than it must be integer. Just take that number and do (mod 10) 5 times and there are Your individual numbers. Then just compare first and fifth, and, second and fourth, and there You go.
Vladan
[This message has been edited by Vladan Radovanovic (edited November 10, 2000).]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is some code that can check the command line argument to see if it is a palindrome.
public class TestForPalindrome
{
public static void main (String [] args)
{
StringBuffer sb = new StringBuffer (args[0]);
StringBuffer reverse = sb.reverse();
String s = sb.toString();

if (args[0].equals(s))
{
System.out.println ("String is a palindrome");
}
else
{
System.out.println ("String is not a palindrome");
}
}
}
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!