JavaK Your free source for Java Knowledge! AFRIKAANS  
     
  Homepage Contact Us Articles Projects Q and A Links  
     
 

Numeric Converter

The numeric converter project is a typical first project for Java students. It accepts a number from the command line and parses the number. If it is a valid decimal number the program continues, if not, it terminates execution with an error message. In the event that no arguments were specified the program will also terminate with an error message.

Once the number has been validated, the program outputs the decimal, hexadecimal, octal and binary values of the given number to the console. This project relies heavily on the built-in functions of the Integer class and is a good example of how to convert numbers between different formats.

Source code for Converter.java
/**
 * Sample project that converts a decimal number to a hexadecimal
 * and binary number.
 *
 * Converter is a single-class application and is aimed at novice students
 * who have just started their Java course.
 */
public class Converter
{
    /**
     * Main method for the Converter class.
     */
    public static void main (String arguments[])
    {

        /**
         * First we ensure that the user has provided us with
         * a parameter that we can use to do the conversion with.
         */
        if (arguments.length == 0)
        {
            System.out.println ("Usage :");
            System.out.println ("javac Converter ");
            System.exit (1);
        }

        /**
         * Now that we have a parameter, let's try to convert it
         * to a number. If we fail, we give an error message and
         * terminate program execution.
         */
        int number = 0;

        try
        {
            number = Integer.parseInt (arguments [0]);
        }
        catch (NumberFormatException e)
        {
            System.out.println ("The number specified is not a valid decimal!");
            System.exit (2);
        }

        /**
         * Once we get here we know the number is a valid decimal number.
         * Output the different formats.
         */
        System.out.print ("The number specified is  : ");
        System.out.println (number);

        System.out.print ("The hexadecimal value is : ");
        System.out.println (Integer.toHexString (number));

        System.out.print ("The binary value is      : ");
        System.out.println (Integer.toBinaryString (number));

        System.out.print ("The octal value is       : ");
        System.out.println (Integer.toOctalString (number));

    }
}






The Author
This article was freely contributed by Ewald Horn to JavaK in the interest of all Java students. Please visit
JavaK for more free Java articles. The author also maintains another website at www.nofuss.co.za.


JavaK is a free website dedicated to providing quality resources to Java students in grades 10 to 12 and beyond. We offer free source code, articles and other information ideal for homework exercises, school projects and assignments. Our main sponsor is NoFuss Solutions, a leading Java development and training solutions provider in the Boland area.