• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Palindromes  RSS feed

 
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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi franklin,
I tried yr problem and following method can be used for number palidromes
a) Use Modulus operator to store the different digits in a seperate array
b) Then compare the elements to check whether the number is palindrome or not.
// program palindrome
class palindrome
{
int num=15351; //u can change this integer
public static void main(String args[])
{
int i;
palindrome p = new palindrome();
int numdivide[] = new int[5];
i=0;
while(p.num > 0)
{
numdivide[i++] = p.num%10;
p.num = p.num / 10;
}
if(numdivide[0]==numdivide[4])
{
if(numdivide[1]==numdivide[3])
System.out.println("Entered number is palindrome");
else
System.out.println("Entered number is not a Palindrome");
}
}
}

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what will u do if it is string the logic will change na
u have to use charAt if it is string by looping it in for between 0 and the length of the string for the palindrom
------------------
I.K.VISHWANATH
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Franklin-
You're getting some good advice here.
When I worked on some similar problem when learning java, I found it helpful to refer to the java docs: http://java.sun.com/j2se/1.3/docs/api/index.html
If you go there you can check out all the methods available to a String; among those methods is charAt as mentioned by Kasi. You will see that String is handled as an array of characters, and there are several ways to manipulate that. You will also need such methods if you ever have to parse a String into a properly formatted phone number or some similar thing.
 
I am displeased. You are no longer allowed to read this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!