8 Replies - 813 Views - Last Post: 01 March 2013 - 11:10 PM Rate Topic: -----

#1 jetset22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,908
  • 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


Was This Post Helpful? 1
  • +
  • -

#3 jetset22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,908
  • 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 ?
Was This Post Helpful? 1
  • +
  • -

#5 jetset22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,908
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • 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? ;)
Was This Post Helpful? 0
  • +
  • -

#8 flareback  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8002
  • View blog
  • Posts: 13,711
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1