Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,399 Java Programmers. There are 1,476 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

the string problem

 
Closed TopicStart new topic

the string problem, roman numeral problem

Rating  5
spit-king
post 6 May, 2008 - 04:49 PM
Post #1


New D.I.C Head

*
Joined: 1 Mar, 2008
Posts: 12



CODE

import java.util.Scanner;

public class RomanNum {      
        public static void main (String [] args) {
                String [] ones = new String [10];
                        ones [0] = "";
                        ones [1] = "I";
                        ones [2] = "II";
                        ones [3] = "III";
                        ones [4] = "IV";
                        ones [5] = "V";
                        ones [6] = "VI";
                        ones [7] = "VII";
                        ones [8] = "VIII";
                        ones [9] = "IX";
                        
                String [] tens = new String [10];
                        tens [0] = "";
                        tens [1] = "X";
                        tens [2] = "XX";
                        tens [3] = "XXX";
                        tens [4] = "XL";
                        tens [5] = "L";
                        tens [6] = "LX";
                        tens [7] = "LXX";
                        tens [8] = "LXXX";
                        tens [9] = "XC";
                        
                String [] hundreds = new String [10];
                        hundreds [0] = "";
                        hundreds [1] = "C";
                        hundreds [2] = "CC";
                        hundreds [3] = "CCC";
                        hundreds [4] = "CD";
                        hundreds [5] = "D";
                        hundreds [6] = "DC";
                        hundreds [7] = "DCC";
                        hundreds [8] = "DCCC";
                        hundreds [9] = "CM";
                        
                String [] thousands = new String [4];
                        thousands [0] = "";
                        thousands [1] = "M";
                        thousands [2] = "MM";
                        thousands [3] = "MMM";
                        
                String [][] all = new String [4][];
                        all [0] = ones;
                        all [1] = tens;
                        all [2] = hundreds;
                        all [3] = thousands;
                        
                String roman = "";
                        
                Scanner input = new Scanner(System.in);
                
        System.out.print("Enter a year, and I will convert it to roman numerals: ");
        int year = input.nextInt();
        
        String yearStr = Integer.toString(year);
        int length = yearStr.length();
        
        for (int i = 0; i < length; i++) {
                roman += all[length - i - 1][Integer.valueOf(yearStr.substring(i,i+1))];
        }
        System.out.print(roman + "\n");
        }
}

i have done this roman numeral problem but i saw the question wrong and i input integer number and output the roman numeral but it should've been the other way roman numeral should be a input and output should be a integer number
so i tried to change it but i couldn't figure out so can you help me out guys

here is the question:
Write a program that reads a string containing a Roman numeral representing a value in the range 1 to 3999. The program should print the Roman numeral and its value in our notation (a Hindu-Arabic numeral).

Roman numerals use the symbols: I = 1; V = 5; X = 10; L = 50; C = 100; D = 500; M = 1000.
Roman numerals use an additive rather than a positional notation. Numerals are formed by writing symbols from the preceding list, from left to right, to represent a sum, each time using the symbol for the largest possible value from the list of symbols. This rule is subject to the limitation that no symbol may be written more than 3 times in a row. To avoid this, the systems uses: IV = 4; IX = 9; XL = 40; XC = 90; CD = 400; and CM = 900. Input is from the keyboard, output to the screen.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


pbl
post 6 May, 2008 - 05:44 PM
Post #2


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 849

That should do the trick....
I let you write the GUI

CODE


public class Roman {

    public static void main(String[] arg) {
        char[] symbols = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
        int[] values = {1, 5, 10, 50, 100, 500, 1000};
        
        String input = "MMDIV";
        
        // make it upperCase
        input = input.toUpperCase();
        // convert String to an arrayOf char
        char[] digit = input.toCharArray();
        // make an array of int to keep the value of each
        int[] each = new int[digit.length];
        
        // scan all the digits read
        for(int i = 0; i < digit.length; i++) {
          each[i] = -1;   // to detect if not found
            // find the value of this particular digit
            for(int j = 0; j < symbols.length; j++) {
                if(digit[i] == symbols[j]) {
                    each[i] = values[j];
                   break;
                }
            }
                               // dtect not fount
              if(each[i] == -1)
                  System.out.println("Invalid character: " + digit[i]);
        }
        // calculate total
        int total = 0;
        // add the other ones unless it is smaller then the immediate follower
        for(int i = 0; i < each.length-1; i++) {
            if(each[i] >= each[i+1])
                total += each[i];
            else
                total -= each[i];
        }
        // end the last one
        total += each[each.length-1];
        
        System.out.println(input + " = " + total);
    }
}


This post has been edited by pbl: 6 May, 2008 - 08:30 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

spit-king
post 6 May, 2008 - 07:28 PM
Post #3


New D.I.C Head

*
Joined: 1 Mar, 2008
Posts: 12

thank you but what's GUI...?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 6 May, 2008 - 07:31 PM
Post #4


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 849

QUOTE(spit-king @ 6 May, 2008 - 07:28 PM) *

thank you but what's GUI...?


User Interface:
The prompt to the user and the reading of the string from the Scanner.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 6 May, 2008 - 08:21 PM
Post #5


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 849

QUOTE(pbl @ 6 May, 2008 - 07:31 PM) *

QUOTE(spit-king @ 6 May, 2008 - 07:28 PM) *

thank you but what's GUI...?


User Interface:
The prompt to the user and the reading of the string from the Scanner.

There is a bug in posted version
We have to check if the number entered is IV or XC
So if the first symbol is < than the first one

CODE

        // calculate total
        int total = each[0];
        if(each.length > 1) {
            if(total < each[1])
                total = -total;
        }
        for(int i = 1; ......

or more simply (finally the first one as to be treated as the others)
     int total = 0;
     for(int i = 0; .....


I will corect the code in the original post

This post has been edited by pbl: 6 May, 2008 - 08:42 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

spit-king
post 8 May, 2008 - 05:29 AM
Post #6


New D.I.C Head

*
Joined: 1 Mar, 2008
Posts: 12

thanks a lot!!!
sorry for the late reply...

This post has been edited by spit-king: 8 May, 2008 - 05:30 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Closed TopicStart new topic
Time is now: 5/17/08 06:15AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month