Total members 11894 |It is currently Thu Nov 21, 2024 9:21 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





/**
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);
}
}

}




Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  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     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com