I'm writing a program that is used to convert binary input to octal and hexadecimal numbers. It is very basic program so I can only use loops, charAt, substrings, length.
Your program logic may process the input by taking groups of 3 digits for a single octal digit or 4 digits for a single hexadecimal digit.
Here is my code. Please help me finish it. It's 100 point project so I cannot miss it
// declare variable String binaryNum, conversionType; int i, j, k; double result = 0; String groupNum; // create scanner object Scanner keyboard = new Scanner(System.in); // prompt user for input System.out.print("Enter a binary number: "); binaryNum = keyboard.next(); for (i = 0; i < binaryNum.length(); i++) { if ((binaryNum.charAt(i) != '1' && (binaryNum.charAt(i) != '0'))) { System.out.println("Error: Incorrect input. Binary numbers only use digits 0 and 1. " + "Reenter you input."); System.out.print("Enter a binary number: "); binaryNum = keyboard.next(); }// end of if }// end of for loops System.out.println("Enter the conversion type(Octal or Hexa): "); conversionType = keyboard.next(); while (true) { if (conversionType.equalsIgnoreCase("octal")) { for (j = 0; j < binaryNum.length(); j += 3) { groupNum = binaryNum.substring(j, j + 3); for (k = 0; k < groupNum.length(); k++) { result += groupNum.charAt(k) * Math.pow(2, k); }//end of for loops }// end of for loops System.out.println(result); System.exit(0); }// end of if }//end of while loops
Many thanks,