View Single Post
  #2 (permalink)  
Old 03-08-2008, 06:39 PM
hardwired hardwired is online now
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
Code:
import java.util.*; public class ConversionsRx { public static void main(String[] args) { // double choice; Scanner input = new Scanner(System.in); System.out.println("[1] Hex to binary. [2] Binary to decimal. " + "[3] Binary to hex. [4] Decimal to any."); // The nextLine method will allow for blocking // execution down the line. The nextInt method may not. int choice = Integer.parseInt(input.nextLine()); /* if(choice == 1) { String hexNums; System.out.println("Enter Hex number: "); hexNums = input.nextLine(); } else if(choice == 2) { System.out.println("Enter Binary number: "); int binNums = input.nextInt(); } else if(choice == 3) { System.out.println("Enter Binary number: "); String binNumss = input.nextLine(); } else if(choice == 4) { System.out.println("Enter Decimal number: "); int decNums = input.nextInt(); System.out.println("Enter Base wanted: "); int desiredBases = input.nextInt(); } */ // A switch argument must be or evaluate-to an int. // Giving it a double won't work. switch(choice) { case 1: // hex to binary // String hexNums; System.out.println("Enter Hex number: "); String hexNums = input.nextLine(); System.out.println("Hex to binary of " + hexNums + " = " + BaseConverter.fromHexToBin(hexNums)); break; case 2: // binary to decimal System.out.println("Enter Binary number: "); // The fromBinToDec method takes a String argument. // int binNums = input.nextInt(); String binStr = input.nextLine(); System.out.println("Binary to decimal of " + binStr + " = " + BaseConverter.fromBinToDec(binStr)); break; case 3: // binary to hex System.out.println("Enter Binary number: "); String binNumss = input.nextLine(); System.out.println("Binary to hex of " + binNumss + " = " + BaseConverter.fromBinToHex(binNumss)); break; case 4: // decimal to any System.out.println("Enter Decimal number: "); int decNums = input.nextInt(); System.out.println("Enter Base wanted: "); int desiredBases = input.nextInt(); System.out.println("Decimal value of " + decNums + " to base " + desiredBases + " = " + BaseConverter.fromDecToAny(decNums, desiredBases)); break; } /** * This block looks like a demo to show you * how to use the BaseConverter class. */ /* //The next 2 lines are used in all versions. System.out.println("ABCD1234 Base-16 converts to " + BaseConverter.fromHexToBin("ABCD1234") +" in Base-2.\n"); System.out.println("E12B47F5 Base-16 converts to " + BaseConverter.fromHexToBin("E12B47F5") +" in Base-2.\n"); //The next 2 lines are used in the 90-point versions and above. System.out.println("1011111011101111 Base-2 converts to " + BaseConverter.fromBinToDec("1011111011101111") +" in Base-10.\n"); System.out.println("11100100100110110 Base-2 converts to " + BaseConverter.fromBinToDec("11100100100110110") +" in Base-10.\n"); //The next line is used in the 95-point versions and above. System.out.println("1011111011101111 Base-2 converts to " + BaseConverter.fromBinToHex("1011111011101111") + " in Base-16.\n"); //The next line is used in the 100-point versions and above. System.out.println("11100100100110110 Base-2 converts to " + BaseConverter.fromBinToHex("11100100100110110") + " in Base-16.\n"); //The next 3 lines are useed in the 105 and 110-point versions. System.out.println("1000 Base-10 converts to " + BaseConverter.fromDecToAny(1000,5) + " in Base-5.\n"); System.out.println("1000 Base-10 converts to " + BaseConverter.fromDecToAny(1000,8) + " in Base-8.\n"); System.out.println("200 Base-10 converts to " + BaseConverter.fromDecToAny(200,2) + " in Base-2.\n"); //This last line is only used in the 110 point version. System.out.println("48879 Base-10 converts to " + BaseConverter.fromDecToAny(48879,16) + " in Base-16.\n"); */ } }
Reply With Quote