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