Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Devin Ashcraft
- * Base conversions
- * OCCC Fall 2106
- * Adv Java
- * */
- import java.math.BigInteger;
- import java.util.Scanner;
- public class BaseConvMain {
- public static void main(String[] args){
- Scanner s = new Scanner(System.in); //int scanner
- String originalBase, finalBase;
- int originalNum = 10, finalNum = 0; // for javac
- int i=0;
- //Welcome message
- System.out.println("Welcome to the Base Conversion machine.");
- //Recieves the first Base
- System.out.println("Please enter a string of numbers or letters.");
- originalBase = s.nextLine();
- //removes all spaces from string
- while(i < originalBase.length()){
- char ch = originalBase.charAt(i);
- if( ch == ' '){
- String before = originalBase.substring(0, i);
- String after = originalBase.substring(i + 1);
- originalBase = before + after;
- }
- else {
- i++;
- }
- }
- // Makes sure string is Uppercase
- originalBase = originalBase.toUpperCase();
- // displays base with no spaces
- System.out.println("You entered: " + originalBase + ".");
- // Recieves first base number
- System.out.println("Please enter the value of the initial base.");
- System.out.println("The base is the highest value you entered 0-9, or if using letters highest A-Z");
- originalNum = s.nextInt();
- // Displays num
- System.out.println("The base you entered was: " + originalNum);
- // validate that base and num work together
- ValidateBase(originalNum);
- isValidInteger(originalBase, originalNum);
- // Gets base that we are converting to
- System.out.println("Please enter the base we are converting to (1-36)");
- finalNum = s.nextInt();
- // do conversion
- finalBase = convertInteger(originalBase, originalNum, finalNum);
- //print finalBase
- System.out.println("The base after conversion is:" + finalBase);
- s.close();
- }
- //Checks that the base entered by user is between 2-36
- private static void ValidateBase(int num){
- if (num < 2 || num > 36){
- System.out.println("Invalid base. Please retry the program.");
- System.exit(0);
- }
- }
- // Checks that the user base works with the given string
- public static boolean isValidInteger(String base, int num){
- char digit;
- for(int i =0; i < base.length(); i++){
- digit = base.charAt(i);
- //Checks if highest digit is not greater than base
- if (Character.isDigit(digit) && (digit - '0') >= num){
- System.out.print("Digit: " +digit + " and Base: " + num + " cannot work together.");
- System.exit(0);
- }
- //checks if highest letter is not greater than base
- else if(Character.isLetter(digit) && (digit - 'A')>= num){
- System.out.print(digit + " and " + num + " cannot work together.");
- System.exit(0);
- }
- //Discards if input isnt 0-9 or A-Z
- else if(!Character.isLetter(digit) && !Character.isDigit(digit)){
- System.out.print("Input is not 0-9, A-Z. Bad input: " + digit);
- System.exit(0);
- }
- }
- return true;
- }
- //Converts original string into user entered base
- // origNum, origBase, newBase
- public static String convertInteger(String base, int num, int finalNum){
- BigInteger decDigit = BigInteger.valueOf(0); // for javac
- BigInteger value = BigInteger.valueOf(0); // for javac
- BigInteger numBI = BigInteger.valueOf(num);
- BigInteger finalNumBI = BigInteger.valueOf(finalNum);
- BigInteger power;
- char digit;
- for (int j = 0; j <base.length(); j++){
- digit = base.charAt(base.length() - 1 - j);
- if(Character.isLetter(digit)){
- decDigit = BigInteger.valueOf(digit - 'A' +10);
- }
- else if(Character.isDigit(digit)){
- decDigit = BigInteger.valueOf(digit - '0');
- }
- value = value.add(decDigit.multiply(numBI.pow(j)));
- //value += decDigit * BigInteger.valueOf(Math.pow((double)num, j));
- }
- int n = 1;
- //for( ; Math.pow(finalNum, n) <= value; n++) {}
- int res = finalNumBI.pow(n).compareTo(value);
- for( ; res >= 0; n++) {}
- char[] result = new char[n];
- for(int k = n-1; k >= 0; k--){
- //power = Math.pow(finalNum, k);
- power = finalNumBI.pow(k);
- //decDigit = Math.floor(value / power);
- decDigit = value.divide(power);
- //value -= decDigit*power;
- value = value.subtract(decDigit.multiply(power));
- int res2 = decDigit.compareTo(BigInteger.valueOf(9));
- if(res2 <= 0){
- result[n-1-k] =(char)('0' + decDigit.intValue());
- }
- else{
- result[n-1-k] =(char)('A' + decDigit.intValue()-10);
- }
- }
- return String.valueOf(result); // returns char array as as string
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement