Too complicated. Make Java do the work for you. Also, you can't easily do base(n) arithmatic with ints because they're always base(10). Use the Integer class instead.
To add two numbers in any base and get the result in that base:public String sum(String num1, String num2, int base)
{
// Convert both numbers to base 10 and add them.
int num1base10 = Integer.parseInt(num1, base);
int num2base10 = Integer.parseInt(num2, base);
int sumInBase10 = num1base10 + num2base10;
// Return the result in the given base.
return Integer.toString(sumInBase10, base);
}
To get the palindrome of a String:public String getPalindrome(String s)
{
return (new StringBuffer(s)).reverse().toString();
}
These can then be used in a loop that simulates what you were doing before:public void Base_N(String num, String num1, int base) {
int count= 0;
System.out.println("Base " + base);
String sNum = num;
String sNum1 = num1;
while (true)
{
count++;
String result = sum(sNum, sNum1, base);
System.out.println(num + "+" + num1 + "=" + result);
if (result.equals(getPalindrome(result)))
break;
sNum = result;
sNum1 = getPalindrome(result);
}
System.out.println(count + " Steps");
}
and call it as follows:Base b = new Base();
b.Base_N("87", "78", 9);
b.Base_N("87", "78", 11);
b.Base_N("abc0", "abc1", 14);
It's very rough code and can be broken easily (because I didn't do any test-first coding) but I think it does approximately what you want.