Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. /** Devin Ashcraft
  2. * Base conversions
  3. * OCCC Fall 2106
  4. * Adv Java
  5. * */
  6.  
  7. import java.math.BigInteger;
  8. import java.util.Scanner;
  9.  
  10.  
  11. public class BaseConvMain {
  12. public static void main(String[] args){
  13. Scanner s = new Scanner(System.in); //int scanner
  14. String originalBase, finalBase;
  15. int originalNum = 10, finalNum = 0; // for javac
  16. int i=0;
  17.  
  18. //Welcome message
  19. System.out.println("Welcome to the Base Conversion machine.");
  20.  
  21. //Recieves the first Base
  22. System.out.println("Please enter a string of numbers or letters.");
  23. originalBase = s.nextLine();
  24.  
  25. //removes all spaces from string
  26. while(i < originalBase.length()){
  27. char ch = originalBase.charAt(i);
  28. if( ch == ' '){
  29. String before = originalBase.substring(0, i);
  30. String after = originalBase.substring(i + 1);
  31. originalBase = before + after;
  32. }
  33. else {
  34. i++;
  35. }
  36. }
  37.  
  38. // Makes sure string is Uppercase
  39. originalBase = originalBase.toUpperCase();
  40.  
  41. // displays base with no spaces
  42. System.out.println("You entered: " + originalBase + ".");
  43.  
  44. // Recieves first base number
  45. System.out.println("Please enter the value of the initial base.");
  46. System.out.println("The base is the highest value you entered 0-9, or if using letters highest A-Z");
  47. originalNum = s.nextInt();
  48. // Displays num
  49. System.out.println("The base you entered was: " + originalNum);
  50.  
  51. // validate that base and num work together
  52. ValidateBase(originalNum);
  53. isValidInteger(originalBase, originalNum);
  54.  
  55. // Gets base that we are converting to
  56. System.out.println("Please enter the base we are converting to (1-36)");
  57. finalNum = s.nextInt();
  58.  
  59. // do conversion
  60. finalBase = convertInteger(originalBase, originalNum, finalNum);
  61.  
  62. //print finalBase
  63. System.out.println("The base after conversion is:" + finalBase);
  64. s.close();
  65. }
  66.  
  67. //Checks that the base entered by user is between 2-36
  68. private static void ValidateBase(int num){
  69. if (num < 2 || num > 36){
  70. System.out.println("Invalid base. Please retry the program.");
  71.  
  72.  
  73. System.exit(0);
  74. }
  75. }
  76.  
  77. // Checks that the user base works with the given string
  78. public static boolean isValidInteger(String base, int num){
  79.  
  80. char digit;
  81.  
  82. for(int i =0; i < base.length(); i++){
  83. digit = base.charAt(i);
  84. //Checks if highest digit is not greater than base
  85. if (Character.isDigit(digit) && (digit - '0') >= num){
  86. System.out.print("Digit: " +digit + " and Base: " + num + " cannot work together.");
  87. System.exit(0);
  88. }
  89.  
  90. //checks if highest letter is not greater than base
  91. else if(Character.isLetter(digit) && (digit - 'A')>= num){
  92. System.out.print(digit + " and " + num + " cannot work together.");
  93. System.exit(0);
  94. }
  95. //Discards if input isnt 0-9 or A-Z
  96. else if(!Character.isLetter(digit) && !Character.isDigit(digit)){
  97. System.out.print("Input is not 0-9, A-Z. Bad input: " + digit);
  98. System.exit(0);
  99. }
  100. }
  101.  
  102. return true;
  103. }
  104.  
  105. //Converts original string into user entered base
  106. // origNum, origBase, newBase
  107. public static String convertInteger(String base, int num, int finalNum){
  108.  
  109. BigInteger decDigit = BigInteger.valueOf(0); // for javac
  110. BigInteger value = BigInteger.valueOf(0); // for javac
  111. BigInteger numBI = BigInteger.valueOf(num);
  112. BigInteger finalNumBI = BigInteger.valueOf(finalNum);
  113. BigInteger power;
  114.  
  115. char digit;
  116.  
  117.  
  118.  
  119. for (int j = 0; j <base.length(); j++){
  120. digit = base.charAt(base.length() - 1 - j);
  121.  
  122. if(Character.isLetter(digit)){
  123. decDigit = BigInteger.valueOf(digit - 'A' +10);
  124. }
  125. else if(Character.isDigit(digit)){
  126. decDigit = BigInteger.valueOf(digit - '0');
  127. }
  128.  
  129. value = value.add(decDigit.multiply(numBI.pow(j)));
  130. //value += decDigit * BigInteger.valueOf(Math.pow((double)num, j));
  131. }
  132.  
  133. int n = 1;
  134.  
  135. //for( ; Math.pow(finalNum, n) <= value; n++) {}
  136. int res = finalNumBI.pow(n).compareTo(value);
  137. for( ; res >= 0; n++) {}
  138. char[] result = new char[n];
  139.  
  140.  
  141.  
  142.  
  143. for(int k = n-1; k >= 0; k--){
  144.  
  145.  
  146. //power = Math.pow(finalNum, k);
  147. power = finalNumBI.pow(k);
  148. //decDigit = Math.floor(value / power);
  149. decDigit = value.divide(power);
  150. //value -= decDigit*power;
  151. value = value.subtract(decDigit.multiply(power));
  152.  
  153. int res2 = decDigit.compareTo(BigInteger.valueOf(9));
  154. if(res2 <= 0){
  155. result[n-1-k] =(char)('0' + decDigit.intValue());
  156. }
  157. else{
  158. result[n-1-k] =(char)('A' + decDigit.intValue()-10);
  159.  
  160. }
  161. }
  162.  
  163. return String.valueOf(result); // returns char array as as string
  164.  
  165.  
  166.  
  167.  
  168. }
  169.  
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement