Results 1 to 7 of 7
Thread: Base Conversion
 12102010, 03:01 PM #1Member
 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.
Thanks in advance!
Code:
Java Code://*************** //Adam Brewer * //Advanced Lab 2* //*************** import java.util.Scanner; class SumOfBaseKDigits { public static void main(String[] args) { Scanner read = new Scanner(System.in); 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: "); testNum = read.nextInt(); System.out.print("Enter a base: "); base = read.nextInt(); 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(); } }
 12102010, 05:39 PM #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));
 12102010, 07:17 PM #3Member
 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?
 12102010, 07:40 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
 12102010, 07:49 PM #5Member
 Join Date
 Sep 2010
 Posts
 56
 Rep Power
 0
Oh, right. JoaSH, do you see the logic error?
 12102010, 07:59 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
 12102010, 08:32 PM #7Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 15
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.
Similar Threads

Java with Data Base
By Nerijus in forum New To JavaReplies: 9Last Post: 05212010, 11:54 AM 
data base connectivity
By zeeshan in forum AWT / SwingReplies: 3Last Post: 02212010, 02:11 AM 
base conversion (large)
By billymac00 in forum New To JavaReplies: 1Last Post: 04072009, 05:56 PM 
[SOLVED] Base conversions
By rlzyoner in forum New To JavaReplies: 3Last Post: 12102008, 01:40 PM 
Connection to data base
By Daniel in forum JDBCReplies: 2Last Post: 06072007, 04:55 AM
Bookmarks