# converting numbers to roman numerals

gaine chung

Greenhorn

Posts: 6

posted 14 years ago

Hello everyone!

I have just started an Intro to JAVA course and I already have an assignment due the 2nd class day. The assignment is to write a program that can convert an integer (from the user's input) to roman numerals. I'm having some problems with it because I don't know how to concatenate strings. I know I may be going at this all wrong, but I've only had about a week's time to figure this out. Here's what I have so far (in semi-pseudocode form):

(M=1000, D=500, C=100, L=50, X=10, V=5, I=1)

String inputNumber= User's input

romanNum= Roman Numeral string

int number= inputNum converted to integer

counterM, counterC, counterX= Counters

If (number>=1000) //I'm thinking I may not need "If" statements

While (number >= 1000)

number -= 1000

counterM += 1

If (counterM >= 1)

While (counterM >= 1)

romanNum += "M"

counterM -= 1

If (number >= 900)

number -= 900

romanNum += "CM"

If (number >= 500)

number -= 500

romanNum += "D"

etc, etc...

As I stated earlier, I can't get the "M" to concatenate with the "CM", "D", etc. Can somebody please help. Thanks.

I have just started an Intro to JAVA course and I already have an assignment due the 2nd class day. The assignment is to write a program that can convert an integer (from the user's input) to roman numerals. I'm having some problems with it because I don't know how to concatenate strings. I know I may be going at this all wrong, but I've only had about a week's time to figure this out. Here's what I have so far (in semi-pseudocode form):

(M=1000, D=500, C=100, L=50, X=10, V=5, I=1)

String inputNumber= User's input

romanNum= Roman Numeral string

int number= inputNum converted to integer

counterM, counterC, counterX= Counters

If (number>=1000) //I'm thinking I may not need "If" statements

While (number >= 1000)

number -= 1000

counterM += 1

If (counterM >= 1)

While (counterM >= 1)

romanNum += "M"

counterM -= 1

If (number >= 900)

number -= 900

romanNum += "CM"

If (number >= 500)

number -= 500

romanNum += "D"

etc, etc...

As I stated earlier, I can't get the "M" to concatenate with the "CM", "D", etc. Can somebody please help. Thanks.

posted 14 years ago

It looks like you are doing String concatenation correctly... += works fine with Strings... are you perhaps not initializing your romanNum value? romanNum = ""; will start you out with an empty String that you can add to...

If this doesn't help you out any, could you give more information on what exactly is happening?

-Nate

If this doesn't help you out any, could you give more information on what exactly is happening?

-Nate

-Nate

Write once, run anywhere, because there's nowhere to hide! - /. A.C.

Michael Fitzmaurice

Ranch Hand

Posts: 168

posted 14 years ago

The way you are concatenating the characters into the String seems to be fine. The following approach is based on the way you had started to do it. It seems to work okay, although I haven't tested it extensively...

<code>

<pre>

public class RomanNumeralConverter

{

private static final char ONE_THOUSAND = 'M';

private static final char FIVE_HUNDRED = 'D';

private static final char ONE_HUNDRED = 'C';

private static final char FIFTY = 'L';

private static final char TEN = 'X';

private static final char FIVE = 'V';

private static final char ONE = 'I';

public static void main(String[] args)

{

int intValue = Integer.parseInt(args[0]);

int remainder = intValue;

String numeralValue = "";

while(remainder >= 1000)

{

numeralValue += ONE_THOUSAND;

remainder -= 1000;

}

while(remainder >= 500)

{

if(remainder >= 900 )

{

numeralValue += ONE_HUNDRED;

numeralValue += ONE_THOUSAND;

remainder -= 900;

break;

}

numeralValue += FIVE_HUNDRED;

remainder -= 500;

}

while(remainder >= 100)

{

if(remainder >= 400 )

{

numeralValue += ONE_HUNDRED;

numeralValue += FIVE_HUNDRED;

remainder -= 400;

break;

}

numeralValue += ONE_HUNDRED;

remainder -= 100;

}

while(remainder >= 50)

{

if(remainder >= 90 )

{

numeralValue += TEN;

numeralValue += ONE_HUNDRED;

remainder -= 90;

break;

}

numeralValue += FIFTY;

remainder -= 50;

}

while(remainder >= 10)

{

if(remainder >= 40)

{

numeralValue += TEN;

numeralValue += FIFTY;

remainder -= 40;

break;

}

numeralValue += TEN;

remainder -= 10;

}

while(remainder >= 5)

{

if(remainder >= 9)

{

numeralValue += ONE;

numeralValue += TEN;

remainder -= 9;

break;

}

numeralValue += FIVE;

remainder -= 5;

}

while(remainder >= 1)

{

if(remainder >= 4)

{

numeralValue += ONE;

numeralValue += FIVE;

break;

}

numeralValue += ONE;

remainder -= 1;

}

System.out.println(intValue + " IN ROMAN NUMERALS: " + numeralValue);

}

}

</pre>

</code>

Hope this helps

Michael

------------------

"One good thing about music - when it hits, you feel no pain"

Bob Marley

[This message has been edited by Michael Fitzmaurice (edited September 17, 2001).]

[This message has been edited by Michael Fitzmaurice (edited September 17, 2001).]

<code>

<pre>

public class RomanNumeralConverter

{

private static final char ONE_THOUSAND = 'M';

private static final char FIVE_HUNDRED = 'D';

private static final char ONE_HUNDRED = 'C';

private static final char FIFTY = 'L';

private static final char TEN = 'X';

private static final char FIVE = 'V';

private static final char ONE = 'I';

public static void main(String[] args)

{

int intValue = Integer.parseInt(args[0]);

int remainder = intValue;

String numeralValue = "";

while(remainder >= 1000)

{

numeralValue += ONE_THOUSAND;

remainder -= 1000;

}

while(remainder >= 500)

{

if(remainder >= 900 )

{

numeralValue += ONE_HUNDRED;

numeralValue += ONE_THOUSAND;

remainder -= 900;

break;

}

numeralValue += FIVE_HUNDRED;

remainder -= 500;

}

while(remainder >= 100)

{

if(remainder >= 400 )

{

numeralValue += ONE_HUNDRED;

numeralValue += FIVE_HUNDRED;

remainder -= 400;

break;

}

numeralValue += ONE_HUNDRED;

remainder -= 100;

}

while(remainder >= 50)

{

if(remainder >= 90 )

{

numeralValue += TEN;

numeralValue += ONE_HUNDRED;

remainder -= 90;

break;

}

numeralValue += FIFTY;

remainder -= 50;

}

while(remainder >= 10)

{

if(remainder >= 40)

{

numeralValue += TEN;

numeralValue += FIFTY;

remainder -= 40;

break;

}

numeralValue += TEN;

remainder -= 10;

}

while(remainder >= 5)

{

if(remainder >= 9)

{

numeralValue += ONE;

numeralValue += TEN;

remainder -= 9;

break;

}

numeralValue += FIVE;

remainder -= 5;

}

while(remainder >= 1)

{

if(remainder >= 4)

{

numeralValue += ONE;

numeralValue += FIVE;

break;

}

numeralValue += ONE;

remainder -= 1;

}

System.out.println(intValue + " IN ROMAN NUMERALS: " + numeralValue);

}

}

</pre>

</code>

Hope this helps

Michael

------------------

"One good thing about music - when it hits, you feel no pain"

Bob Marley

[This message has been edited by Michael Fitzmaurice (edited September 17, 2001).]

[This message has been edited by Michael Fitzmaurice (edited September 17, 2001).]

"One good thing about music - when it hits, you feel no pain" <P>Bob Marley

gaine chung

Greenhorn

Posts: 6

posted 14 years ago

Thanks for the help guys!

Nathan: As it turns out, I failed to initialize my string value: romanNum. I thought initializing was only for numbers. Silly me.

Michael: That's a very interesting approach. Too bad I'm not experienced enough to go about it your way. In due time...

Well, here's my final approach (for anyone who's interested). Again, thanks for the help.

_________________________________

import javax.swing.JOptionPane;

public class ConvertYear {

public static void main(String args[]) {

int year,

showYear;

String strYear,

romanNum;

romanNum = "";

strYear = JOptionPane.showInputDialog("Please type in a year");

year = Integer.parseInt(strYear);

showYear = year;

while (year >= 1000) {

year -= 1000;

romanNum += "M";

}

if (year >= 900) {

year -= 900;

romanNum += "CM";

}

if (year >= 500) {

year -= 500;

romanNum += "D";

}

if (year >= 400) {

year -= 400;

romanNum += "CD";

}

while (year >= 100) {

year -= 100;

romanNum += "C";

}

if (year >= 90) {

year -= 90;

romanNum += "XC";

}

if (year >= 50) {

year -= 50;

romanNum += "L";

}

if (year >= 40) {

year -= 40;

romanNum += "XL";

}

while (year >= 10) {

year -= 10;

romanNum += "X";

}

if (year == 9) {

year -= 9;

romanNum += "IX";

}

if (year >= 5) {

year -= 5;

romanNum += "V";

}

if (year == 4) {

year -= 4;

romanNum += "IV";

}

while (year >= 1) {

year -=1;

romanNum += "I";

}

JOptionPane.showMessageDialog(null, "The Roman Numeral equivalent of " + showYear + " is " + romanNum);

System.exit(0);

}

}

Nathan: As it turns out, I failed to initialize my string value: romanNum. I thought initializing was only for numbers. Silly me.

Michael: That's a very interesting approach. Too bad I'm not experienced enough to go about it your way. In due time...

Well, here's my final approach (for anyone who's interested). Again, thanks for the help.

_________________________________

import javax.swing.JOptionPane;

public class ConvertYear {

public static void main(String args[]) {

int year,

showYear;

String strYear,

romanNum;

romanNum = "";

strYear = JOptionPane.showInputDialog("Please type in a year");

year = Integer.parseInt(strYear);

showYear = year;

while (year >= 1000) {

year -= 1000;

romanNum += "M";

}

if (year >= 900) {

year -= 900;

romanNum += "CM";

}

if (year >= 500) {

year -= 500;

romanNum += "D";

}

if (year >= 400) {

year -= 400;

romanNum += "CD";

}

while (year >= 100) {

year -= 100;

romanNum += "C";

}

if (year >= 90) {

year -= 90;

romanNum += "XC";

}

if (year >= 50) {

year -= 50;

romanNum += "L";

}

if (year >= 40) {

year -= 40;

romanNum += "XL";

}

while (year >= 10) {

year -= 10;

romanNum += "X";

}

if (year == 9) {

year -= 9;

romanNum += "IX";

}

if (year >= 5) {

year -= 5;

romanNum += "V";

}

if (year == 4) {

year -= 4;

romanNum += "IV";

}

while (year >= 1) {

year -=1;

romanNum += "I";

}

JOptionPane.showMessageDialog(null, "The Roman Numeral equivalent of " + showYear + " is " + romanNum);

System.exit(0);

}

}

gaine chung

Greenhorn

Posts: 6

Paul Stevens

Ranch Hand

Posts: 2823

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |