java - Changing palindrome to fit numeric palindrome -


so i've figured out pretty effective way check palindromes, asked change fit numeric palindrome , again stuck.

public boolean checknumericpalindrome() {  string s = this.s.tolowercase(); string resultstring = ""; for(int i=0; i<s.length(); i++) {       if(character.isdigit(s.charat(i))) {          resultstring = resultstring + s.charat(i);       }     } int low = 0; int high = resultstring.length() - 1;  boolean ispalindrome = true;  while (low < high) {   if (resultstring.charat(low) != resultstring.charat(high)) {     ispalindrome = false;     break;   }    low++;   high--;     } return ispalindrome; } 

`

i've figured out had change letters isdigits, , know should change or rid of tolowercase , s.length can't seem find numeric equivalent, or know if there one.

if don't want make major changes logic, can make string input number , same thing.

public boolean checknumericpalindrome(int number) {         string resultstring = integer.tostring(number);         //proceed same logic & process string         return true;     } 

edit:
better approach this:

public boolean checknumericpalindrome(int number) {      return checkstringpalindrome(integer.tostring(number)); } 

so whenever make changes checkstringpalindrome, don't need change other methods.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -