# How can I determine if a 5 digit number is a palindrome using an array

Page 1 of 1

## 8 Replies - 1080 Views - Last Post: 01 March 2013 - 11:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=313897&amp;s=d5dab8d2c461f31a90be78b46418e9e8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jetset22

Reputation: 0
• Posts: 3
• Joined: 28-February 13

# How can I determine if a 5 digit number is a palindrome using an array

Posted 28 February 2013 - 08:11 PM

I've searched everywhere and have found lots of examples on how to tell if it is a palindrome but none showing how to do it with an array. So far I have,
``` public static void main(String[] args) {
while (true) {
display(check(retrieveInput()));
}
}

public static String retrieveInput() {
Scanner scan = new Scanner(System.in);
return scan.next();
}

public static boolean check(String input) {
boolean check = false;
try {
Integer.parseInt(input);
if (input.charAt(0)==input.charAt(4) && input.charAt(1)==input.charAt(3))
check = true;

} catch(Exception e) {
check = false;
}

return check;
}

public static void display(boolean check) {
if(check) System.out.println("Is a five-digit palindrome.");
else System.out.println("Is not a five-digit palindrome.");

```

I'm not sure how to include an array in this. Any help is greatly appreciated

Is This A Good Question/Topic? 0

## Replies To: How can I determine if a 5 digit number is a palindrome using an array

### #2 pbl

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

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: How can I determine if a 5 digit number is a palindrome using an array

Posted 28 February 2013 - 08:15 PM

Don't hardcode make a loop

```   String str = "laval";
boolean palyn = true;
int len = str.length();
for(int i = 0 ; i < len / 2; i++) {
if(str.charAt(i) != str.charAt(--len)) {
palyn = false;
break;
}
}
if(palyn) the stringr is a palydrome

```

### #3 jetset22

Reputation: 0
• Posts: 3
• Joined: 28-February 13

## Re: How can I determine if a 5 digit number is a palindrome using an array

Posted 28 February 2013 - 08:17 PM

Thanks! I feel like a dummy. You wouldn't believe how hard it was to finally find someone willing to help. Thanks again!I am trying it now.

### #4 pbl

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

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: How can I determine if a 5 digit number is a palindrome using an array

Posted 28 February 2013 - 08:19 PM

Wonder what type of Exception you were tryng to catch ? A Russian trawler ?

### #5 jetset22

Reputation: 0
• Posts: 3
• Joined: 28-February 13

## Re: How can I determine if a 5 digit number is a palindrome using an array

Posted 28 February 2013 - 08:29 PM

lol. no spy ships here. Excuse my naivete, but do I still need a scanner when employing your method of doing things?

### #6 pbl

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

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: How can I determine if a 5 digit number is a palindrome using an array

Posted 28 February 2013 - 08:35 PM

You need a Scanner to read the input from the user scanner.nextLine()
that's it

### #7 g00se

• D.I.C Lover

Reputation: 3214
• Posts: 14,150
• Joined: 20-September 08

## Re: How can I determine if a 5 digit number is a palindrome using an array

Posted 01 March 2013 - 05:28 AM

Quote

but none showing how to do it with an array.
So where's the array?

### #8 flareback

Reputation: 3
• Posts: 30
• Joined: 27-February 13

## Re: How can I determine if a 5 digit number is a palindrome using an array

Posted 01 March 2013 - 10:53 PM

How are you getting the values into an array in the first place. You can either take the input one digit at a time, inserting into the next spot in the array each time through. Or you can get all the input at once and convert it to a char array and do your comparison with that. Using a char array would be a fairly easy way to get it into an array since there is a method for going from a string to a char array. After that it's just a little modification of pbl's code.

### #9 jon.kiparsky

• Pancakes!

Reputation: 9447
• Posts: 16,369
• Joined: 19-March 11

## Re: How can I determine if a 5 digit number is a palindrome using an array

Posted 01 March 2013 - 11:10 PM

Seems silly to go to the trouble of parsing the input to an int if you're just going to split it up into an array. The String class has a method which will return the underlying char array that backs the String - you can use this and just compare the chars. (or you can just use pbl's code directly )

Otherwise, you could work on the number directly: write a method which returns the digitAt(index) and use pbl's notion. How do you get the digitAt some index? Well, there's a little arithmetic there, it makes a nice little challenge for you. You'll use the % and / operators, and Math.pow will also be useful.