1
+ import java .util .Arrays ;
2
+ import java .util .HashSet ;
1
3
import java .util .InputMismatchException ;
2
4
import java .util .Scanner ;
3
5
4
6
/**
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.
7
10
*
8
11
* @author Michael Rolland
9
- * @version 2017.09.29
12
+ * @version 2017.10.10
10
13
*
11
14
*/
12
15
public class AnyBaseToAnyBase {
13
16
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
+
14
21
// Driver
15
22
public static void main (String [] args ) {
16
23
Scanner in = new Scanner (System .in );
17
- System .out .print ("Enter number: " );
18
- String n = in .nextLine ();
24
+ String n ;
19
25
int b1 =0 ,b2 =0 ;
20
26
while (true ) {
21
27
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 +"): " );
23
31
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 +"): " );
25
41
b2 = in .nextInt ();
42
+ if (b2 > MAXIMUM_BASE || b2 < MINIMUM_BASE ) {
43
+ System .out .println ("Invalid base!" );
44
+ continue ;
45
+ }
26
46
break ;
27
47
} catch (InputMismatchException e ) {
28
48
System .out .println ("Invalid input." );
@@ -32,6 +52,29 @@ public static void main(String[] args) {
32
52
System .out .println (base2base (n , b1 , b2 ));
33
53
}
34
54
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
+
35
78
/**
36
79
* Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal,
37
80
* then decimal to b2.
0 commit comments