Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping

Status
Not open for further replies.

markpanther

Technical User
Joined
Sep 6, 2002
Messages
2
Location
US
Can anyone help me w this code some
Im tryin to convert digits to roman numerals and vice versa
Code:
import javax.swing.JOptionPane;
[COLOR=red] public class ExerRoman {
	String arabicString = new String(arabic);	String romanString = new String("");
	String aLen = arabicString.length;
	int n = 1;
	while ( n <= aLen ){
	   int i = ( aLen - n );
	   int s = parseInt(arabicString.charAt(i));
	if (n == 1)
 { romanString = RomanI[s]; }	
	if (n == 2) 
{ romanString = RomanX[s]+romanString; }			if (n == 3) 
{ romanString = RomanC[s]+romanString; }			if (n == 4) 
{ romanString = RomanM[s]+romanString; }			n++;	
	return romanString;}	
//read in first user input	
String arabic =	
JOptionPane.showInputDialog( &quot;Enter first number&quot; );
[/color][COLOR=blue]
int RomanI = new Array(&quot;&quot;,&quot;I&quot;,&quot;II&quot;,&quot;III&quot;,&quot;IV&quot;,&quot;V&quot;,&quot;VI&quot;,&quot;VII&quot;,&quot;VIII&quot;,&quot;IX&quot;);

int RomanX = new Array(&quot;&quot;,&quot;X&quot;,&quot;XX&quot;,&quot;XXX&quot;,&quot;XL&quot;,&quot;L&quot;,&quot;LX&quot;,&quot;LXX&quot;,&quot;LXXX&quot;,&quot;XC&quot;);

int RomanC = new Array(&quot;&quot;,&quot;C&quot;,&quot;CC&quot;,&quot;CCC&quot;,&quot;CD&quot;,&quot;D&quot;,&quot;DC&quot;,&quot;DCC&quot;,&quot;DCCC&quot;,&quot;CM&quot;);

int RomanM = new Array(&quot;&quot;,&quot;M&quot;,&quot;MM&quot;,&quot;MMM&quot;,&quot;IV&quot;,&quot;V&quot;,&quot;VI&quot;,&quot;VII&quot;,&quot;VIII&quot;,&quot;IX&quot;);
[/color]
	JOptionPane.showMessageDialog (		null, &quot;The product is &quot; + romanString, &quot;Romanized&quot;,	JOptionPane.PLAIN_MESSAGE );}
 
Can someone show me how to fix this
Code:
    public class RomanNumeral {   
        private final int num; 
         private static int[]   
 numbers =
{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5 , 4, 1) ;                       
 private static String[] letters = { &quot;M&quot;,  &quot;CM&quot;,  &quot;D&quot;,  &quot;CD&quot;, &quot;C&quot;,  &quot;XC&quot;, &quot;L&quot;,  &quot;XL&quot;,  &quot;X&quot;,  &quot;IX&quot;, &quot;V&quot;,  &quot;IV&quot;, &quot;I&quot; };   
     public RomanNumeral(int arabic) {   
       if (arabic < 1)     
        throw new NumberFormatException(&quot;Value of     RomanNumeral must be positive.&quot;);  
        if (arabic > 3999)    
        throw new NumberFormatException(&quot;Value of RomanNumeral must be 3999 or less.&quot;); 
         num = arabic;   
    }    

   public String toRoman() {  
        String roman = &quot;&quot;;    
       int n = num;     
             for (int i = 0; i < numbers.length; i++) {             while (n >= numbers<i>) {                roman += letters<i>;   
            
 n -= numbers<i>; 
            } 
         }    
      return roman;   
    }  
  }




import javax.swing.JOptionPane;

public class ExerRoman {   
   public static void main(String args[]) {  
      String arabic = JOptionPane.showInputDialog
           ( &quot;Enter first number&quot; );  
      RomanNumeral n = new RomanNumeral(Integer.parseInt (arabic));       
     String romanString = n.toRoman();
        JOptionPane.showMessageDialog 
(null, &quot;The product is &quot; + romanString, &quot;Romanized&quot;,JOptionPane.PLAIN_MESSAGE );        System.exit(0); 
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top