Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,482 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,762 people online right now. Registration is fast and FREE... Join Now!




help converting Roman to decimal

 

help converting Roman to decimal

bosancero

5 Nov, 2008 - 11:58 AM
Post #1

New D.I.C Head
*

Joined: 5 Nov, 2008
Posts: 5


Hi I am new Java user with zero experience writing codes. The issue (beside other smile.gif) I have is that my entered Roman numerals work only if the numerlas are entered in large to smaller format. For exapmple if I type MMC program will correctly display 2100, but when I enter MCM it will display 2100 instead of 1900. I know that I need to come up with a way to determine what letter comes before so program can subtract it but at this point am little stuck. Here is my code:

CODE

package chapt08;


import javax.swing.JOptionPane;

/* Write a program that converts a number entered in Roman numerals to decimal. Your program should consists

* of a class, say Roman. An objects of the types Roman should do the following:

* a) store the number as Roman numeral

* B) Convert and store the number into decimal

* c) print the number as a Roman numeral or decimal number as requested by the user

*

* The decimal values of the Roman numerals are

* M 1000

* D 500

* C 100

* L 50

* X 10

* V 5

* I 1

*

* d) test your program using the following Roman numerals: MCXIV, CCCLIX and MDCLXVI

*

*/


public class Roman

{



static String romanNumeral;

static int decimalNum;

static char convertToDecimal;


public static void main(String args[])

{


Roman demo = new Roman();

demo.convertToDecimal(decimalNum);

printRoman(romanNumeral);

printDecimal(decimalNum);

setRoman();

toString(romanNumeral);

}



public void convertToDecimal (int other)

{

romanNumeral = JOptionPane.showInputDialog("Enter a Roman numeral to convert to decimal." + "\n\n"+ "Note: Roman numerals are I, V, X, L, C, D and M."+ "\n" + "All letters entered will be treated as capitalized.");

romanNumeral = romanNumeral.toUpperCase();


int x = 0;

do

{


convertToDecimal = romanNumeral.charAt(x); //return a char value from the first character in the string


switch(convertToDecimal)

{

case 'M':

decimalNum += 1000;

break;

case 'D':

decimalNum += 500;

break;

case 'C':

decimalNum += 100;

break;

case 'L':

decimalNum += 50;

break;

case 'X':

decimalNum += 10;

break;

case 'V':

decimalNum += 5;

break;

case 'I':

decimalNum += 1;

break;

}

x++;

}while(x<romanNumeral.length());


//make a condition here...

JOptionPane.showMessageDialog(null, "Roman Numeral Converted to Decimal is: " + decimalNum);// + ch,



//other=decimalNum.getCopy;


}


public static void printRoman (String romanNumeral){


System.out.println ("Roman Numeral stored is: " + romanNumeral);


}

public static void printDecimal (int decimalNum){


System.out.println ("Decimal Number stored is: " + decimalNum);

}

public static void setRoman (){

System.out.println("test Set Roman");


}


// public boolean equals (String romanNumeral, int decimalNum ){

// return (romanNumeral == decimalNum);

// }


public void Other(String temp)

{

temp = "";

}

public Roman getCopy(int decimalNum)

{

Roman temp = new Roman();

Roman.decimalNum = decimalNum;


System.out.println(" temp is: " + temp);

return temp;

}


public static String toString(String str)

{

//str = "test";

if (str==romanNumeral)

System.out.println("Roman numeral " + romanNumeral + " is equal to Decimal Number " + decimalNum);

else

System.out.println("Roman numeral " + romanNumeral + " is not equal to Decimal Number " + str);

return str;

}


}



User is offlineProfile CardPM
+Quote Post


bosancero

RE: Help Converting Roman To Decimal

5 Nov, 2008 - 11:57 AM
Post #2

New D.I.C Head
*

Joined: 5 Nov, 2008
Posts: 5

I apologize for not including code in brackets. Sorry.

CODE


package chapt08;


import javax.swing.JOptionPane;

/* Write a program that converts a number entered in Roman numerals to decimal. Your program should consists

* of a class, say Roman. An objects of the types Roman should do the following:

* a) store the number as Roman numeral

* b) Convert and store the number into decimal

* c) print the number as a Roman numeral or decimal number as requested by the user

*

* The decimal values of the Roman numerals are

* M 1000

* D 500

* C 100

* L 50

* X 10

* V 5

* I 1

*

* d) test your program using the following Roman numerals: MCXIV, CCCLIX and MDCLXVI

*

*/


public class Roman

{



static String romanNumeral;

static int decimalNum;

static char convertToDecimal;


public static void main(String args[])

{


Roman demo = new Roman();

demo.convertToDecimal(decimalNum);

printRoman(romanNumeral);

printDecimal(decimalNum);

setRoman();

toString(romanNumeral);

}



public void convertToDecimal (int other)

{

romanNumeral = JOptionPane.showInputDialog("Enter a Roman numeral to convert to decimal." + "\n\n"+ "Note: Roman numerals are I, V, X, L, C, D and M."+ "\n" + "All letters entered will be treated as capitalized.");

romanNumeral = romanNumeral.toUpperCase();


int x = 0;

do

{


convertToDecimal = romanNumeral.charAt(x); //return a char value from the first character in the string


switch(convertToDecimal)

{

case 'M':

decimalNum += 1000;

break;

case 'D':

decimalNum += 500;

break;

case 'C':

decimalNum += 100;

break;

case 'L':

decimalNum += 50;

break;

case 'X':

decimalNum += 10;

break;

case 'V':

decimalNum += 5;

break;

case 'I':

decimalNum += 1;

break;

}

x++;

}while(x<romanNumeral.length());


//make a condition here...

JOptionPane.showMessageDialog(null, "Roman Numeral Converted to Decimal is: " + decimalNum);// + ch,



//other=decimalNum.getCopy;


}


public static void printRoman (String romanNumeral){


System.out.println ("Roman Numeral stored is: " + romanNumeral);


}

public static void printDecimal (int decimalNum){


System.out.println ("Decimal Number stored is: " + decimalNum);

}

public static void setRoman (){

System.out.println("test Set Roman");


}


// public boolean equals (String romanNumeral, int decimalNum ){

// return (romanNumeral == decimalNum);

// }


public void Other(String temp)

{

temp = "";

}

public Roman getCopy(int decimalNum)

{

Roman temp = new Roman();

Roman.decimalNum = decimalNum;


System.out.println(" temp is: " + temp);

return temp;

}


public static String toString(String str)

{

//str = "test";

if (str==romanNumeral)

System.out.println("Roman numeral " + romanNumeral + " is equal to Decimal Number " + decimalNum);

else

System.out.println("Roman numeral " + romanNumeral + " is not equal to Decimal Number " + str);

return str;

}


}



User is offlineProfile CardPM
+Quote Post

pbl

RE: Help Converting Roman To Decimal

5 Nov, 2008 - 05:31 PM
Post #3

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
Never heard od indentation... your code is not easy to read....
And all these empty lines.. I have to scroll 4 times my destop screen to see all your code

Here is the code re-vamped... may be somebody will now have a look at it

java

import javax.swing.JOptionPane;

/* Write a program that converts a number entered in Roman numerals to decimal. Your program should consists
* of a class, say Roman. An objects of the types Roman should do the following:
* a) store the number as Roman numeral
* cool.gif Convert and store the number into decimal
* c) print the number as a Roman numeral or decimal number as requested by the user
*
* The decimal values of the Roman numerals are
* M 1000
* D 500
* C 100
* L 50
* X 10
* V 5
* I 1
*
* d) test your program using the following Roman numerals: MCXIV, CCCLIX and MDCLXVI
*
*/
public class Roman {
static String romanNumeral;
static int decimalNum;
static char convertToDecimal;

public static void main(String args[]) {
Roman demo = new Roman();
demo.convertToDecimal(decimalNum);
printRoman(romanNumeral);
printDecimal(decimalNum);
setRoman();
toString(romanNumeral);
}

public void convertToDecimal (int other) {
romanNumeral = JOptionPane.showInputDialog("Enter a Roman numeral to convert to decimal." + "\n\n"+ "Note: Roman numerals are I, V, X, L, C, D and M."+ "\n" + "All letters entered will be treated as capitalized.");
romanNumeral = romanNumeral.toUpperCase();

int x = 0;

do
{
convertToDecimal = romanNumeral.charAt(x); //return a char value from the first character in the string
switch(convertToDecimal)
{
case 'M':
decimalNum += 1000;
break;

case 'D':
decimalNum += 500;
break;

case 'C':
decimalNum += 100;
break;

case 'L':
decimalNum += 50;
break;

case 'X':
decimalNum += 10;
break;

case 'V':
decimalNum += 5;
break;

case 'I':
decimalNum += 1;
break;
}

x++;
}while(x<romanNumeral.length());

// make a condition here...
JOptionPane.showMessageDialog(null, "Roman Numeral Converted to Decimal is: " + decimalNum);// + ch,
// other=decimalNum.getCopy;
}

public static void printRoman (String romanNumeral){
System.out.println ("Roman Numeral stored is: " + romanNumeral);
}

public static void printDecimal (int decimalNum){
System.out.println ("Decimal Number stored is: " + decimalNum);
}

public static void setRoman (){
System.out.println("test Set Roman");
}

// public boolean equals (String romanNumeral, int decimalNum ){
// return (romanNumeral == decimalNum);
// }

public void Other(String temp) {
temp = "";
}

public Roman getCopy(int decimalNum) {
Roman temp = new Roman();
Roman.decimalNum = decimalNum;

System.out.println(" temp is: " + temp);
return temp;
}

public static String toString(String str) {
// str = "test";
if (str==romanNumeral)
System.out.println("Roman numeral " + romanNumeral + " is equal to Decimal Number " + decimalNum);
else
System.out.println("Roman numeral " + romanNumeral + " is not equal to Decimal Number " + str);

return str;
}
}


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:03AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month