Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: numerical conversion methods..

  1. #1
    Junior Member Neo's Avatar
    Join Date
    Feb 2011
    Location
    Ohio
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default numerical conversion methods..

    ok here is a good question that i can't find an answer too. ANYWHERE. i have the following code.
    import java.util.Scanner; //Imports the Scanner Device
    public class MethodsProject {
     
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in); //names the scanner device
            int number; //declares an integer
            int decimal; //declares an integer
            int total; //declares an integer
            int input; //declares an integer
            String input2; //declares a String
            final String BINARY = "Binary"; //sets a constant string called Binary
            final String OCTAL = "Octal"; //sets a constant string called Octal
            final String HEX = "Hex"; //sets a constant string called Hex
            final String EXIT = "Exit"; //sets a constant called Exit
            String conversionType;
     
            do {
                conversionType = getConversionType(keyboard); //directs the program to the getConversionType method
     
                if (conversionType.equalsIgnoreCase(BINARY)) { //opens if binary body
                    System.out.print("Enter the binary number to be converted:");
                    input = keyboard.nextInt(); //gets the number from user
                    decimal = getBinary(keyboard, input); //sends the input to the getBinary method
                    System.out.printf("Binary number " + input + " converted to"
                            + " decimal is: " + decimal + "\n");
                    keyboard.nextLine(); //clears the buffer
                } //closes the if binary body
                if (conversionType.equalsIgnoreCase(OCTAL)) { //opens the if octal body
                    System.out.printf("Enter the number to be converted: ");
                    input = keyboard.nextInt(); //gets the number to be converted from user
                    decimal = getOctal(keyboard, input); //throws input to the getOctal method
                    System.out.printf("Octal number " + input + " converted to "
                            + "decimal is: " + decimal + "\n");
                    keyboard.nextLine();// clears the buffer
                } //ends the if octal body
                else if (conversionType.equalsIgnoreCase(HEX)) { //opens the if HEX body
                    System.out.println("Enter the Number to be converted: ");
                    input2 = keyboard.nextLine(); //gets the next string input
                    // decimal = getHex(keyboard, input2);
                    //System.out.printf("Hex number " + input + " converted to " +
                    //        " decimal is: " + decimal + "\n");
                    keyboard.nextLine(); //clears the buffer
     
                } //closes the if Hex body
            } while (!conversionType.equalsIgnoreCase(EXIT)); //ends the do-while loop
     
     
     
     
     
     
     
     
     
     
     
        } //ends main method
     
        /**
         * Get the type of conversion to be used
         * pre-condition: given valid scanner to get input from
         * post-condition: returns a conversion type of either Octal, Binary, or Hex
         * @param: a scanner representing the keyboard
         * @return: returns the conversion type
         */
        public static String getConversionType(Scanner keyboard) {//opens the body of getConversion
            String conversionType;
            final String BINARY = "Binary"; //sets a constant string called Binary
            final String OCTAL = "Octal"; //sets a constant string called Octal
            final String HEX = "Hex"; //sets a constant string called Hex
            final String EXIT = "Exit"; //sets a constant called Exit
            do { //opens a do-while loop
                System.out.printf("Enter the conversion type: (BINARY, OCTAL, HEX)"
                        + " if done type EXIT:");
                conversionType = keyboard.nextLine(); //allocates the user input to the memory slot called conversion
                if (!conversionType.equalsIgnoreCase(BINARY) //runs a check on the input for validity
                        && !conversionType.equalsIgnoreCase(OCTAL)
                        && !conversionType.equalsIgnoreCase(HEX)
                        && !conversionType.equalsIgnoreCase(EXIT)) {
                    System.out.println("ERROR: Wrong Conversion Type.");
     
                } //ends the if statement
            } while (!conversionType.equalsIgnoreCase(BINARY)
                    && !conversionType.equalsIgnoreCase(OCTAL)
                    && !conversionType.equalsIgnoreCase(HEX)
                    && !conversionType.equalsIgnoreCase(EXIT));
            return conversionType; //returns the inputed conversion type
        } //ends getConversionType method
     
        /**
         * Get the type of conversion to be used
         * pre-condition: given valid scanner input for conversion
         * post-condition: returns a value in decimal form
         * @param: a scanner representing the keyboard
         * @return: returns the decimal form of the Binary number inputed
         */
        public static int getBinary(Scanner keyboard, int input) { //opens the getBinary method
            int currentValue; //declares an integer
            int power = 0; //declares an integer
            int total = 0; //declares an integer
     
     
     
            for (int i = Integer.toString(input).length() - 1; i >= 0; i--) { //starts for loop
                currentValue = Integer.parseInt(Integer.toString(input).charAt(i) + ""); //evaluates the next int in the line
                if (currentValue > 1 || currentValue < 0) { //opens if body
                    System.out.println("ERROR: The non Binary number: "
                            + input + " was entered. \n The program is ending.");
                    System.exit(1); //directs the program to the runExit Method
                } //ends if statement
                total += currentValue * Math.pow(2, power); //creates a new total for each new value
                power++; //updates power to the next step
            } //ends for loop
     
            return total; //returns total
        } //ends getBinary mehtod
     
        /**
         * Get the type of conversion to be used
         * pre-condition: given valid scanner input for conversion
         * post-condition: returns a value in decimal form
         * @param: a scanner representing the keyboard
         * @return: returns the decimal form of the Octal number inputed
         */
        public static int getOctal(Scanner keyboard, int input) { //opens the body of getOctal method
            int currentValue; //declares an integer
            int power = 0;//declares an integer
            int total = 0; //declares an integer
     
     
     
            for (int i = Integer.toString(input).length() - 1; i >= 0; i--) { //starts the for loop
                currentValue = Integer.parseInt(Integer.toString(input).charAt(i) + ""); //gets a starting value
                if (currentValue > 8 || currentValue < 0) { //opens if body
                    System.out.println("ERROR: The non Octal number: "
                            + input + " was entered. \n The program is ending.");
                    System.exit(1); //directs the program to the runExit Method
                } //ends if statement
                total += currentValue * Math.pow(8, power); //creates a new value for total
                power++; //updates power for the next step
            } //ends for loop
     
            return total; //returns the total in decimal form
        } //ends getOctal method
     
        /**
         *Get the type of conversion to be used
         * pre-condition: given valid scanner input for conversion
         * post-condition: returns a value in decimal form
         * @param: a scanner representing the keyboard
         * @return: returns the decimal form of the Hex string inputed
         */
        public static String getHex(Scanner keyboard, String input2) { //opens the getHex method
            int currentValue;
     
            for (int i = input2.length() - 1; i >= 0; i--) { //starts the for loop
                currentValue = input2.charAt(i) + ""); //gets a starting value
                if (currentValue > 8 || currentValue < 0) { //opens if body
                    System.out.println("ERROR: The non Hex digit: "
                            + input2 + " was entered. \n The program is ending.");
                    System.exit(1); //directs the program to the runExit Method
                } //ends if statement
            }
            return currentValue; //returns the value
        } //ends the getHex method
    } //ends class

    now my teacher wants us to convert any user input from whatever form the user decides to decimal. trick is we can't use any pre-defined library codes. i.e. hexCharToDecimal, etc. we have to write the source code all by ourselves. i have binary and octal done no problem. i can't for the life of me find or figure out hex. this will be just for reference fior future becuase i have like an hour to finish this. so if anyone has any idea i would like their input


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: numerical conversion methods..

    In the future, please post your own topic instead of hijacking another one (granted, they were very similar topics).

    There are many websites which detail how to perform base conversions by hand (or as a computer algorithm). Here's one such site: Implementation of Base Conversion Algorithms from Interactive Mathematics Miscellany and Puzzles

Similar Threads

  1. [SOLVED] Method of Sorting? by assiging numerical values to Strings
    By Mirage in forum Java Theory & Questions
    Replies: 0
    Last Post: June 16th, 2010, 07:49 PM
  2. JAVA to C++ conversion
    By hackerboy101 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 28th, 2010, 08:31 PM
  3. Explicit Conversion?
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 11th, 2009, 11:40 PM
  4. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 04:29 AM
  5. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM