-2

For the program I need to write, I have to make sure that the user gives me a five-digit number which is in the form of a palindrome. The program must check to make sure that the number is a palindrome, and if it is not, that it gives an error.

Does anyone have any idea how to do something like that? I am using Eclipse, if it helps.
Mostly, I just need help making sure that the number given to the program is only five digits, no more and no less.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
user3348422
  • 1
  • 2
  • 5
  • Use a `regex` or other method to check if it's a 5-digit number, then use the link above to see if it's a palindrome. – AntonH May 22 '14 at 15:31
  • To be honest, I'm pretty sure that when I check for the palindrome, I can just tell the program it needs to check if digit 1 and digit 5 are the same, and if digit 2 and digit 4 are the same. – user3348422 May 22 '14 at 15:31
  • Try reading it in as a string, checking the length of the string, then checking if each character is a digit. Should be able to follow the link @AntonH posted – EyeOfTheHawks May 22 '14 at 15:32
  • 1
    Remember, this is not a place to have people do your homework for you. Show us some code that you have tried, and people here will be more than willing to help you FIX the code you have written or offer suggestions to tweak it. – Sully Brooks May 22 '14 at 15:33
  • Hey this is a pretty valid question... – Anubian Noob May 22 '14 at 15:49

2 Answers2

4
  • Read your data as string
  • check if it contains only digits (if you use regex you can also check length like matches("\\d{5}"))
  • check if it is palindrome by comparing firs character with fifth and second with forth (you can use charAt(index)).
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • +1 for using a String and not an integer. -1 for not providing details or code snippets (yet). All in all, +0. – nrubin29 May 22 '14 at 15:36
  • 4
    @nrubin29 I suspect that this question is homework so I don't want to give OP full working code. Instead portion of pointers should be enough to solve this problem. Anyway thanks for your opinion. – Pshemo May 22 '14 at 15:37
  • 1
    @nrubin29 Well he pretty gives the algorithm. The OP can easily write the program for that. +1 for me, there is no need to give all the code. – Alexis C. May 22 '14 at 15:37
  • @Pshemo Agreed. If OP can't give us any code, why should we. – AntonH May 22 '14 at 15:38
  • 1
    @AntonH That's not a healthy mindset to approach SO... – Anubian Noob May 22 '14 at 15:42
  • The perfect way to answer. @nrubin - algorithm and helpful is better than assuming something is homework and nannying. This IS a help facility and in this day and age kudos for bothering to ask for help. – RichieHH May 22 '14 at 15:43
  • @AntonH : no one is expecting "we" to do anything. As you see some people can be positive and constructive and guide the beginner. If you can help do. Whining about it isnt helpful, positive or an approach that suggests SO is the place to be. – RichieHH May 22 '14 at 15:44
  • @AnubianNoob My comment was a very (too) abrupt way of saying that, if OP seeming hasn't put effort into solving the given problem, nor possibly searching for a solution (the link I gave, right at the outset, is a protected SO article, and is the result of a Google search with 2 words: `java palindrome`), then why should we give a complete code solution. Which is why I agreed with Pshemo's answer, of giving the logic/pseudo-code. But like I said, I was too abrupt. – AntonH May 22 '14 at 15:52
  • We give him an answer because he asked a valid question... That's how SO works... – Anubian Noob May 22 '14 at 15:53
  • To the responses to my comment: I agree with what you are saying. +1. – nrubin29 May 22 '14 at 16:28
3

If you have it as an integer:

int n = getUserInput();

// Number is not 5 digits.
if (n/10000>=10 || n/10000<=0)
    throw new Exception();

// First and last digits don't match.
if (n%10 != n/10000)
    throw new Exception();

// Second and fourth digits don't match.
if ((n%100)/10 != (n/1000)%10)
    throw new Exception();

If you have it as a String:

String s = getUserInput();

// Test that pin is number.
for (int i=0;i<s.length();i++) {
    if (c < '0' || c > '9') // or `if (!Character.isDigit(c))`
        throw new Exception();
}

// String is not 5 characters.
if (s.length() != 5)
    throw new Exception();

// First and last don't match.
if (s.charAt(0) != s.charAt(4))
    throw new Exception();

// Second and fourth don't match
if (s.charAt(1) != s.charAt(3))
    throw new Exception();
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75