Results 1 to 7 of 7

Thread: Base Conversion

  1. #1
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default 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();
    	}
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    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));

  3. #3
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    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 ;-)
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    Oh, right. JoaSH, do you see the logic error?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

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

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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

  1. Java with Data Base
    By Nerijus in forum New To Java
    Replies: 9
    Last Post: 05-21-2010, 11:54 AM
  2. data base connectivity
    By zeeshan in forum AWT / Swing
    Replies: 3
    Last Post: 02-21-2010, 01:11 AM
  3. base conversion (large)
    By billymac00 in forum New To Java
    Replies: 1
    Last Post: 04-07-2009, 05:56 PM
  4. [SOLVED] Base conversions
    By rlzyoner in forum New To Java
    Replies: 3
    Last Post: 12-10-2008, 12:40 PM
  5. Connection to data base
    By Daniel in forum JDBC
    Replies: 2
    Last Post: 06-07-2007, 04:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •