/** WriteBase.java
 * Allows the user to input, from the keyboard, positive ints
 *  using any "base" from binary through decimal (2-10),
 *  and permits the output of positive ints using these bases
 *
 *  @author:  Dr. Henry H. Leitner
 *  @version: Last Modified January 13, 2009
 */

import java.util.*;
import java.io.*;

class WriteBase
{
    static final int BINARY = 2;
    static final int OCTAL = 8;
    static final int DECIMAL = 10;


    /** readBase allows the user to input an int value
     *  from the keyboard.  The value is typed as a 
     *  sequence of chars in some base between binary
     *  and decimal.  Returns the equivalent _decimal_ int.
     *  Does not deal with negative numbers at all.
     */
    static int readBase (int base) throws IOException
    {
        char ch;
        Scanner keyboard = new Scanner(System.in);

        do                      		    // Ignore white space and
        {
            ch = (char) System.in.read(); 	// non-base chars
        }
        while (  (ch < '0' ||  ch -'0' >= base ));
        int n = 0;
        do
         {   // This loop "builds" an int
            n = (base * n) + (ch -'0');
            ch = (char) System.in.read();
         } while ((ch >= '0') && (ch < '0' + base));
		
		// Now toss the rest of the line (may be empty):

	if (ch != '\n') keyboard.nextLine();
        return n;
    }

    /** writeBase is a recursive routine to display
     *  a _decimal_ integer arg 'n' as a sequence of
     *   chars, in some base 'base', between binary and decimal.
     */
    static void writeBase (int n, int base)
    {
        if (n >= base) writeBase( n / base, base );
        writeDigit ( n % base);
         
    }
      
    /** writeDigit prints a single digit character
     *   between '0' and '9' 
     */
    static void writeDigit (int n)
    {
        switch (n) 
        {
            case 0: System.out.print ('0');  break;
            case 1: System.out.print ('1');  break;
            case 2: System.out.print ('2');  break;
            case 3: System.out.print ('3');  break;
            case 4: System.out.print ('4');  break;
            case 5: System.out.print ('5');  break;
            case 6: System.out.print ('6');  break;
            case 7: System.out.print ('7');  break;
            case 8: System.out.print ('8');  break;
            case 9: System.out.print ('9');  break;
        }
     }

    public static void main (String[] args) throws IOException
    {
       int foo;
       System.out.print ("Input a positive DECIMAL integer: ");
       foo = readBase (DECIMAL);
       System.out.print("In DECIMAL, that equals: ");
       writeBase(foo, DECIMAL);  System.out.println();
       System.out.print("In OCTAL, that equals: ");
       writeBase(foo, OCTAL);    System.out.println();
       System.out.print("In BINARY, that equals: ");
       writeBase(foo, BINARY);   System.out.println();
       System.out.println();
       System.out.print ("Input a positive BINARY integer: ");
       foo = readBase (BINARY);
       System.out.print("In DECIMAL, that equals: ");
       writeBase(foo, DECIMAL);  System.out.println();
       System.out.print("In OCTAL, that equals: ");
       writeBase(foo, OCTAL);    System.out.println();
       System.out.print("In BINARY, that equals: ");
       writeBase(foo, BINARY);   System.out.println();
       System.out.println("\n\nThat's all, folks!");
    }
}
