Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 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. double decDigit = 0; // for javac
  110. double value = 0; // for javac
  111. double power;
  112.  
  113. char digit;
  114.  
  115.  
  116.  
  117. for (int j = 0; j <base.length(); j++){
  118. digit = base.charAt(base.length() - 1 - j);
  119.  
  120. if(Character.isLetter(digit)){
  121. decDigit = digit - 'A' +10;
  122. }
  123. else if(Character.isDigit(digit)){
  124. decDigit = digit - '0';
  125. }
  126.  
  127. value += decDigit * Math.pow((double)num, j);
  128. }
  129.  
  130. int n = 1;
  131. /*while(Math.pow(finalNum, n) <= value){
  132. n++;
  133. }*/
  134.  
  135. for( ; Math.pow(finalNum, n) <= value; n++) {}
  136. char[] result = new char[n];
  137.  
  138.  
  139.  
  140.  
  141. for(int k = n-1; k >= 0; k--){
  142.  
  143.  
  144. power = Math.pow(finalNum, k);
  145. decDigit = Math.floor(value / power);
  146. value -= decDigit*power;
  147.  
  148. if(decDigit <= 9){
  149. result[n-1-k] =(char)('0' + (int)decDigit);
  150. }
  151. else{
  152. result[n-1-k] =(char)('A' + (int)decDigit-10);
  153.  
  154. }
  155. }
  156.  
  157. return String.valueOf(result); // returns char array as as string
  158.  
  159.  
  160.  
  161.  
  162. }
  163.  
  164.  
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement