1. Member Join Date
Sep 2010
Posts
56
Rep Power
0

## Base Conversion

I am writing this program for my AP Java Programming class. The assignment is to enter a number 'n' and a number 'k'. The program must convert the number 'n' to base 'k' and then add the sum of the digits.

I have gone through this over and over and cannot figure out my logic error. For example, when I enter "255 base 2", the output is "65" when it should be 8. 255 is base 2 is 11111111, so the sum should be 1+1+1+1+1+1+1+1 = 8.

Can anyone help? I cannot figure out the logic error, and I have asked many of the other students for help (though less experienced in programming than me) and they cannot be of help either.

Code:

Java Code:
```//***************
//***************

import java.util.Scanner;

class SumOfBaseKDigits
{
public static void main(String[] args)
{

int base = 0;
double testNum = 0;
int exponent = 0;
int sum = 0;
int numInBase = 0;

System.out.println("This program will convert a number to a certain base.");
System.out.println("It will then add the converted digits.");

System.out.println();

System.out.print("Enter a test number: ");

System.out.print("Enter a base: ");

double[] numArray = new double[base+1]; //Initializes the array that will hold the expanded base numbers
int[] sumsArray = new int[base+1]; //Initializes the array that will hold the digits in base "b"

for(int e = 0; e < numArray.length; e++)
{
numArray[e] = Math.pow(base, exponent);
exponent++;
}

for(int e = (numArray.length - 1); e >= 0; e--) //Converts the number to base "b"
{
if(testNum / numArray[e] >= 1)
{
sumsArray[e] = (int)(testNum / numArray[e]);
testNum = testNum % numArray[e];
}
}

for(int e = 0; e < sumsArray.length; e++) //Finds the sum of the digits in base "b"
{
sum += sumsArray[e];
}

System.out.println();
System.out.println("The sum of the digits in base " + base + " is " + sum + ".");
//Outputs the sum
System.out.println();
}
}```  Reply With Quote

2. ## i didn't analyze what's wrong with the code but you can find a good conversion example here or if you want to convert only bases of 2, 8 and 15 you can also use the methods of the Integer wrapper class, example

Java Code:
```		int i = 255;
// base 2
System.out.println(Integer.toBinaryString(i));
// base 8
System.out.println(Integer.toOctalString(i));
// base 15
System.out.println(Integer.toHexString(i));```  Reply With Quote

3. Member Join Date
Sep 2010
Posts
56
Rep Power
0

## I see. But I need to convert to any input base; not just 2, 8, 15. Can you find the logic error?  Reply With Quote

4. ##  Originally Posted by j2me64 Java Code:
```		int i = 255;
// base 15
System.out.println(Integer.toHexString(i));```
Erm, sixteen, that's base sixteen, not base fifteen.

kind regards,

Jos ;-)  Reply With Quote

5. Member Join Date
Sep 2010
Posts
56
Rep Power
0

## Oh, right. JoaSH, do you see the logic error?  Reply With Quote

6. ##  Originally Posted by javaman1 Oh, right. JoaSH, do you see the logic error?
Yes, his comment was incorrect.

kind regards,

Jos  Reply With Quote

7. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
17

## do you see the logic error?

Perhaps you could explain the algorithm you are using. Or say what output you get for some input, and what you expected.

-------------------------

It would be a good plan to use a method to do the conversion (and return an array of ints or whatever). That way the logic can be cleanly separated from the prompting and other input and output. So for this problem I would begin by implementing a simple method (of a few lines probably):

Java Code:
```    /**
* Returns the elements making up a given  number in a given base.
*/
private static int[] convert(int num, int base) {
// code here
}```

(Note in terms of finding the digit sum this array can be in descending or ascending place values: whatever is convenient.)

If you are finding the digits of an integer value in some base, why do you declare testNum and numArray as doubles?

Use System.out.println() often to print out the values of the arrays and of the loop indices so you can get a feel for what is happening. In this regard remember:

Java Code:
`System.out.println(Arrays.toString(numArray));`

Why do you make numArray and sumsArray the length you do? If base is 2 this would seem a little confining. Since you are using nextInt() to obtain the number to be converted you should, with a little thought, be able to figure out the maximum number of digits there will ever be.  Reply With Quote

advanced, base, conversion, exponent 