Switch to full style
General Java code examples
Post a reply

Converting arab in Roman Numerals

Mon Nov 18, 2013 10:40 pm

/**
Sample program converting arab numerals into Roman ones. What you think guys, is it
short enough? Which code can I reduce and how can I make it more compact? **/

package Console;

import Numbers.ToFourThousand;

public class Main {
public static void main(String[] arg){

ToFourThousand number = new ToFourThousand();

number.input(2509);

}
}



package Numbers;

public class ToFourThousand
{
//Making a class-constructor.
public ToFourThousand(){};

//Four arrays for units, tents, hundreds and thousands.
String[] oneNine = new String[] { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

String[] tenHundred = new String[] { "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};

String[] hundredThousand = new String[] { "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};

String[] thousands = new String[] { "M", "MM", "MMM" };

public void input(int input)
{
if(input < 1 || input >= 4000)
System.out.println("Sorry, not supported number. Try something between zero and 3999./n
Programmer doesn't have enoguh funds... :/"

int fstNumber = 0;
int sndNumber = 0;
int trdNumber = 0;

//Define thousands
int fthNumber=0;
if(input / 1000 == 1)
fthNumber = 0;
else if(input / 1000 == 2)
fthNumber = 1;
else if(input / 1000 == 3)
fthNumber = 2;


//Define units
if(input % 10 == input)
fstNumber = input;
else if(input % 100 == input)
fstNumber = input%10;
else if(input % 1000 == input)
fstNumber = (input%100)%10;
else if(input % 1000 <= input)
fstNumber = ((input%1000)%100)%10;


//Define tents
if(input % 1000 == input)
if(input % 100 == input)
sndNumber = input/10;
else
sndNumber = (input%100)/10;
else
sndNumber = ((input%1000)%100)/10;

//Define hundreds
if(input % 1000 == input)
trdNumber = input/100;
else
trdNumber = (input%1000)/100;


//Define the array container
String[] number = new String[4];

if(input % 1000 == 0 || input % 1000 < input)
number[0] = thousands[fthNumber];
else if(input % 1000 == input)
number[0] = "";

if(input % 1000 < 1000 && (input%1000)<100)
number[1] = "";
else if(input%1000 == input || (input % 1000 < 1000 && input % 1000 >= 100))
number[1] = hundredThousand[trdNumber - 1];

if((input % 1000 < input && (input%1000)%100 >= 10) || (input%1000 == input &&
input%100 >= 10))
number[2] = tenHundred[sndNumber - 1];
else
number[2] = "";


number[3] = oneNine[fstNumber];


for (String currentNumber : number)
{
if("".equals(currentNumber))
continue;

System.out.print(currentNumber);
}
}

}



Post a reply
  Related Posts  to : Converting arab in Roman Numerals
 ordered list with roman numbers     -  
 Converting Session object into string     -  
 Benefits of converting PowerPoint presentations to DVD     -