1+ import java .util .Arrays ;
2+ import java .util .HashSet ;
13import java .util .InputMismatchException ;
24import java .util .Scanner ;
35
46/**
5- * Class for converting from any base to any other base, though it's unclear how digits greater than
6- * 36 would be represented in bases >36.
7+ * Class for converting from "any" base to "any" other base, when "any" means from 2-36.
8+ * Works by going from base 1 to decimal to base 2. Includes auxiliary method for
9+ * determining whether a number is valid for a given base.
710 *
811 * @author Michael Rolland
9- * @version 2017.09.29
12+ * @version 2017.10.10
1013 *
1114 */
1215public class AnyBaseToAnyBase {
1316
17+ // Smallest and largest base you want to accept as valid input
18+ static final int MINIMUM_BASE = 2 ;
19+ static final int MAXIMUM_BASE = 36 ;
20+
1421 // Driver
1522 public static void main (String [] args ) {
1623 Scanner in = new Scanner (System .in );
17- System .out .print ("Enter number: " );
18- String n = in .nextLine ();
24+ String n ;
1925 int b1 =0 ,b2 =0 ;
2026 while (true ) {
2127 try {
22- System .out .print ("Enter beginning base: " );
28+ System .out .print ("Enter number: " );
29+ n = in .next ();
30+ System .out .print ("Enter beginning base (between " +MINIMUM_BASE +" and " +MAXIMUM_BASE +"): " );
2331 b1 = in .nextInt ();
24- System .out .print ("Enter end base: " );
32+ if (b1 > MAXIMUM_BASE || b1 < MINIMUM_BASE ) {
33+ System .out .println ("Invalid base!" );
34+ continue ;
35+ }
36+ if (!validForBase (n , b1 )) {
37+ System .out .println ("The number is invalid for this base!" );
38+ continue ;
39+ }
40+ System .out .print ("Enter end base (between " +MINIMUM_BASE +" and " +MAXIMUM_BASE +"): " );
2541 b2 = in .nextInt ();
42+ if (b2 > MAXIMUM_BASE || b2 < MINIMUM_BASE ) {
43+ System .out .println ("Invalid base!" );
44+ continue ;
45+ }
2646 break ;
2747 } catch (InputMismatchException e ) {
2848 System .out .println ("Invalid input." );
@@ -32,6 +52,29 @@ public static void main(String[] args) {
3252 System .out .println (base2base (n , b1 , b2 ));
3353 }
3454
55+ /**
56+ * Checks if a number (as a String) is valid for a given base.
57+ */
58+ public static boolean validForBase (String n , int base ) {
59+ char [] validDigits = {'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' ,
60+ 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' ,
61+ 'W' , 'X' , 'Y' , 'Z' };
62+ // digitsForBase contains all the valid digits for the base given
63+ char [] digitsForBase = Arrays .copyOfRange (validDigits , 0 , base );
64+
65+ // Convert character array into set for convenience of contains() method
66+ HashSet <Character > digitsList = new HashSet ();
67+ for (int i =0 ; i <digitsForBase .length ; i ++)
68+ digitsList .add (digitsForBase [i ]);
69+
70+ // Check that every digit in n is within the list of valid digits for that base.
71+ for (char c : n .toCharArray ())
72+ if (!digitsList .contains (c ))
73+ return false ;
74+
75+ return true ;
76+ }
77+
3578 /**
3679 * Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal,
3780 * then decimal to b2.
0 commit comments