All pastes #1199089 Raw Edit

Untitled

public text v1 · immutable
#1199089 ·published 2008-09-10 12:39 UTC
rendered paste body

//----------------------------------------------------------
//Import of the Scanner class allows the user to enter input
//----------------------------------------------------------
import java.util.Scanner;
 
public class BCD
{
		//-----------------------------------------------------------
		//	converts the user inputed strings of decimal digits into
		//	Binary Coded decimal (BCD) strings.
		//-----------------------------------------------------------
        public static void main(String args[])
        {
                final Scanner SC = new Scanner(System.in);//scanner class is declared and instantiated
                String input;//declared for user inputs through the scanner class
                String output;//storage for calculated input which is displayed to the user
                int i;//int storage
               	int last;//int storage for the last character entered in the BCD 
               	int total;//int storage for the total 0s and 1s for each run in the BCD
                
                //--------------------------------------------------------------------------------
       			//Print line explains the conversion process and gives the user prompts to follow 
       			//--------------------------------------------------------------------------------
                System.out.println("Welcome to the conversion program!");
        		System.out.println("it performs coversions on integer decimal numbers \n"
       			+ "which are at least zero.");
        		System.out.println("When asked what to do next, please enter: \n" 
        		+ "'toBCD' to convert to Binary Coded Decimal (BCD). \n"
        		+ "'toRLE' to covert to BCD and also Run Length Encoded (RLE)BCD.");
        		System.out.println("Anything else to stop the program.");
                
                while(true)//evaluates the boolean condition repititively until false
                {
                        System.out.println("What do you want to do next?");//prompts user to interact
                        input = SC.next();//Scanner class method read from the keyboard via user input.
                        
                        //----------------------------------------------------------------------------
                        //Evaluates the statement, which if true, prompts the user to enter an integer
                      	//----------------------------------------------------------------------------
                        if(input.equals("toBCD"))
                        {
                                System.out.println("Please enter the number to be converted.");
                                input = SC.next();
                                
                                //--------------------------------------------------------------------
                                //processes the users input, and if true  format, produces the output,
                                //otherwise it 'catches' the exception and displays the error.
                                //--------------------------------------------------------------------
                                try
                                {		
                                		//--------------------------------------------------------
                                		//returns the int corresponding to the value stored in the
                                		//specified string, in this case, the input string.
                                		//--------------------------------------------------------
                                        Integer.parseInt(input);
                                
                                        output = "";//assigns a value for the variable output to recognise
                                        for(i=0; i<input.length(); i++)
                                        {
                                                switch(input.charAt(i))
                                                {
                                                        case '0': 
                                                           	output = output + "0000";
                                                        	break;
                                                        case '1': 
                                                        	output = output + "0001";
                                                        	break;
                                                        case '2': 
                                                        	output = output + "0010"; 
                                                        	break;
                                                        case '3': 
                                                        	output = output + "0011"; 
                                                        	break;
                                                        case '4': 
                                                        	output = output + "0100"; 
                                                        	break;
                                                        case '5': 
                                                        	output = output + "0101"; 
                                                        	break;
                                                        case '6': 
                                                        	output = output + "0110"; 
                                                        	break;
                                                        case '7': 
                                                        	output = output + "0111"; 
                                                        	break;
                                                        case '8': 
                                                        	output = output + "1000"; 
                                                        	break;
                                                        case '9': 
                                                        	output = output + "1001"; 
                                                        	break;
                                                }
                                        }
                                        
                                        System.out.println("As BCD this is: " + output);
                                }
                                //------------------------------------------------------------------------------
                                //catches the exception thrown by the ParseInt if a valid Integer is not entered
                                //------------------------------------------------------------------------------
                                catch(NumberFormatException e)
                                {
                                        System.out.println("Unfortunately you've entered an invalid number.");
                                }
                        }
                        else if(input.equals("toRLE"))
                        {
                                System.out.println("Please enter the number to be converted.");
                                input = SC.next();
                                
                                try
                                {
                                        Integer.parseInt(input);
                                
                                        output = "";
                                        for(i=0; i<input.length(); i++)
                                        {
                                                switch(input.charAt(i))
                                                {
                                                        case '0': 
                                                        	output = output + "0000"; 
                                                        	break;
                                                        case '1': 
                                                        	output = output + "0001"; 
                                                        	break;
                                                        case '2': 
                                                        	output = output + "0010"; 
                                                        	break;
                                                        case '3': 
                                                        	output = output + "0011"; 
                                                        	break;
                                                        case '4': 
                                                        	output = output + "0100"; 
                                                        	break;
                                                        case '5': 
                                                        	output = output + "0101"; 
                                                        	break;
                                                        case '6': 
                                                        	output = output + "0110"; 
                                                        	break;
                                                        case '7': 
                                                        	output = output + "0111"; 
                                                        	break;
                                                        case '8': 
                                                        	output = output + "1000"; 
                                                        	break;
                                                        case '9': 
                                                        	output = output + "1001"; 
                                                        	break;
                                                        default:							
                                                        	break;
                                                }
                                        }
                                        
                                        System.out.println("As BCD this is: " + output);
                                        
                                        if(output.indexOf("0000000000") != -1 || output.indexOf("1111111111") != -1)
                                        {
                                                System.out.println("Unfortunately I can't run length encode that.");
                                        }
                                        else
                                        {
                                                System.out.print("As RLE this is: ");
                                                
                                                last = 0;
                                                total = 0;
                                                for(i=0; i<output.length(); i++)
                                                {
                                                        switch(output.charAt(i))
                                                        {
                                                                case '0':
                                                                        if(last == 0)
                                                                        {
                                                                                total++;
                                                                        }
                                                                        else
                                                                        {
                                                                                System.out.print("" + total);
                                                                                total = 1;
                                                                                last = 0;
                                                                        }
                                                                break;
                                                                
                                                                case '1':
                                                                        if(last == 1)
                                                                        {
                                                                                total++;
                                                                        }
                                                                        else
                                                                        {
                                                                                System.out.print("" + total);
                                                                                total = 1;
                                                                                last = 1;
                                                                        }
                                                                break;
                                                        }
                                                }
                                                
                                                System.out.println("" + total);
                                        }
                                }
                                catch(NumberFormatException e)
                                {
                                        System.out.println("Unfortunately you've entered an invalid number.");
                                        break;
                                }
                        }
                        else
                        {
                                System.exit(0);
                        }
                }
        }
}