public class StaticCharMethods2
1: // Fig. 29.16: StaticCharMethods2.java
2: // Static Character conversion methods.
3: import java.util.Scanner;
4:
5: public class StaticCharMethods2
6: {
7: // create StaticCharMethods2 object execute application
8: public static void main( String args[] )
9: {
10: Scanner scanner = new Scanner( System.in );
11:
12: // get radix
13: System.out.println( "Please enter a radix:" );
14: int radix = scanner.nextInt();
15:
16: // get user choice
17: System.out.printf( "Please choose one:\n1 -- %s\n2 -- %s\n",
18: "Convert digit to character", "Convert character to digit" );
19: int choice = scanner.nextInt();
20:
21: // process request
22: switch ( choice )
23: {
24: case 1: // convert digit to character
25: System.out.println( "Enter a digit:" );
26: int digit = scanner.nextInt();
27: System.out.printf( "Convert digit to character: %s\n",
28: Character.forDigit( digit, radix ) );
29: break;
30:
31: case 2: // convert character to digit
32: System.out.println( "Enter a character:" );
33: char character = scanner.next().charAt( 0 );
34: System.out.printf( "Convert character to digit: %s\n",
35: Character.digit( character, radix ) );
36: break;
37: } // end switch
38: } // end main
39: } // end class StaticCharMethods2
40:
41: /**************************************************************************
42: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
43: * Pearson Education, Inc. All Rights Reserved. *
44: * *
45: * DISCLAIMER: The authors and publisher of this book have used their *
46: * best efforts in preparing the book. These efforts include the *
47: * development, research, and testing of the theories and programs *
48: * to determine their effectiveness. The authors and publisher make *
49: * no warranty of any kind, expressed or implied, with regard to these *
50: * programs or to the documentation contained in these books. The authors *
51: * and publisher shall not be liable in any event for incidental or *
52: * consequential damages in connection with, or arising out of, the *
53: * furnishing, performance, or use of these programs. *
54: *************************************************************************/