Results 1 to 2 of 2
  1. #1
    chelsid7 is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Unhappy Need some help with decimal to base conversion!!

    I am writing a program to convert a base 10 integer to any base 2-16. Here are the terms:

    "The method is to convert the decimal value the user selected into whatever base the user selected and print the converted value one place value at a time by using an index into an array of characters 0-9 amd A-F that is initialized to contain those characters.
    In the method you will find the largest place value (i.e. power of the base) that will divide into the decimal number.
    Then you can set up a loop that will operate from that power down to and including the 0th power to determine how many times each place value goes into the decimal number. Using the loop counter, index into the character array to print the character that corresponds to the quotient number and then subtract the product of the place value and the quotient from the decimal number. With the power (loop index) decreased, repeat until you finish the 0th place value.

    Here's what I have so far:

    import java.io.*;
    import java.util.Scanner;
    public class ConvertIt
    {//start program
    public static void main(String[] args)
    {//start main
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a positive integer from 0 to 10000.");
    int number = input.nextInt();

    System.out.println("Enter the base to convert to.");
    int base = input.nextInt();

    convertIt(number, base);

    }//end main

    public static void convertIt(int number, int base)
    {//start method
    int array = {0123456789ABCDEF};

    while (number != 0 && < 10000)
    {//start while

    }//end while
    for (int i = array.length-1; i > -1; i--)
    {//start for

    }//end for

    }//end while
    }//end method
    }//end program


    I think I have it set up correctly, I'm just not sure how to attack it. I understand the concept of what I'm doing, just not the execution of it.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Need some help with decimal to base conversion!!

    First, please place your code between code tags [CODE][/CODE]. There are some syntax errors above. The int array is not initialized correctly or with the correct values for an integer array. Also, your while loop conditional is incorrectly constructed. I suggest you fix these and add the algorithm you described and see if it compiles and then if it works. If not, we will still be here to help.

    Examples of correct syntax.

    Java Code:
    int myArray[] = {1,2,3,4,5,6};
    char charArray[] = {'1', '2', '3', '4', 'e', 'f', 'g' };
    
    while (a < 100 && b > 2000) {
    // manipulate a and b
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Binary to Decimal conversion
    By mrashi12 in forum New To Java
    Replies: 4
    Last Post: 12-31-2011, 07:52 PM
  2. Replies: 5
    Last Post: 08-25-2011, 07:32 PM
  3. Base Conversion
    By javaman1 in forum New To Java
    Replies: 6
    Last Post: 12-10-2010, 07:32 PM
  4. base conversion (large)
    By billymac00 in forum New To Java
    Replies: 1
    Last Post: 04-07-2009, 05:56 PM

Posting Permissions

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