Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Roman Numbers

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Roman Numbers

    help here pls.. i'm having problem on how can i conver a roman number into a real number

    heres what i've got so far
    public class RomanNumbers {
     
        private static BufferedReader br = new BufferedReader(new InputStreamReader(
                                           System.in));
     
        public static void main(String[] args) throws IOException {
     
            char temp = 0;
     
            int rN = 0;
     
            int num = 0;
     
            System.out.print("Enter The Roman Number: ");
            String romanNumber = br.readLine();
     
            for (int count = 0; count <= romanNumber.length() - 1; count++) {
     
                temp = romanNumber.charAt(count);
     
                switch (temp) {
     
                    case 'I':
     
                        rN = 1;
     
                        break;
     
                    case 'V':
     
                        rN = 5;
     
                        break;
                }
     
                num = num + rN;
            }
     
            System.out.println(num);
        }
    }

    the problem is when i enetered a roman number for example:

    IV = 4, because I is less than V which is one of the rules in converting a roman number

    if the first number is less than the next one... it should be subtracted... otherwise it will be added...


    my problem is how can i compare the first value of the roman number into the value next to it..


    i dont have any more problems when the sequence is greatest to least.. because i only have to add the values,,
    but im having a hard time thinking of how could i compare the values to get the answer if the first number is less than the next number


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Roman Numbers

    How abou tthis

    import java.util.Scanner;
     
     
    public class Roman {
    	public static void main(String[] args) {
    		Scanner stdIn = new Scanner(System.in);
     
    		System.out.print("Enter a Roman Number:> ");
    		char[] roman = stdIn.nextLine().toCharArray();
     
    		int total = 0;
    		for(int i = roman.length-1; i > -1; i--){
    			switch(roman[i]){
    				case 'I':
    					total += value(roman[i]); break;
    				case 'V':
    				case 'X':
    				case 'L':
    				case 'C':
    				case 'D':
    				case 'M':
    					if(i != 0 && (value(roman[i-1]) < value(roman[i]))){
    						total += value(roman[i]) - value(roman[i-1]);
    						i--;
    					}else{
    						total += value(roman[i]);
    					}
    					break;
    			}
    		}
    		System.out.println(total);
    	}
     
    	public static int value(char c){
    		switch(c){
    			case 'I':
    				return 1;
    			case 'V':
    				return 5;
    			case 'X':
    				return 10;
    			case 'L':
    				return 50;
    			case 'C':
    				return 100;
    			case 'D':
    				return 500;
    			case 'M':
    				return 1000;
    			default:
    				return 0;
    		}
    	}
     
    }

    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    chronoz13 (December 8th, 2009)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Roman Numbers

    Basically what happens here is that for every character you translate you need to check the next character to see if its bigger than the current character and if it is you know that its a subtraction.

    Read the section called "Subtractive principle" on Roman numerals - Wikipedia, the free encyclopedia

    Generally, Roman numerals are written in descending order from left to right, and are added sequentially, for example MMVI (2006) is interpreted as 1000 + 1000 + 5 + 1.
    Certain combinations employ a subtractive principle, which specifies that where a symbol of smaller value precedes a symbol of larger value, the smaller value is subtracted from the larger value, and the result is added to the total. For example, in MCMXLIV (1944), the symbols C, X and I each precede a symbol of higher value, and the result is interpreted as 1000 plus (1000 minus 100) plus (50 minus 10) plus (5 minus 1).
    A numeral for 10n (I, X, or C) may not precede a numeral larger than 10n+1, where n is an integer.[citation needed] That is, I may precede V and X, but not L or C; X may precede L or C, but not D or M. The numerals 5×10n (V, L, or D) may not be followed by a numeral of greater or equal value.[citation needed] Any symbol that appears more than once consecutively may not be followed by a symbol of larger value.
    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (December 8th, 2009)

  6. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Roman Numbers

    chris.. i noticed something in the for statement.... the "length" method , i looks different..
    in my IDE (netbeans) it is color green..


    and color green, statnds for fields or objects... but i dont know where that "length" came from...

  7. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Roman Numbers

    the variable roman is a char Array, in Java these have a variable known as length containing the size of the array.

    Chris

  8. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Roman Numbers

    ahh so thats it.. a method that returns a length of an array of characters.....

    tnx!!

  9. #7
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Roman Numbers

    Not a method, an instance variable

  10. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Roman Numbers

    aw .. sorry....

  11. #9
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Roman Numbers

    Just making sure you now

    Chris

  12. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Roman Numbers

    tnx tnx!

  13. #11
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Roman Numbers

    chris ... just want to ask something about this part...

    			case 'V':
    				case 'X':
    				case 'L':
    				case 'C':
    				case 'D':
    				case 'M':

    ahmm...

    if i pass the characer for example 'V' to the method .value(<char>),
    its doest not return ALL the succeding values after the 'V' right?

    the return value will just depend on the character that i will pass to that method...?

    does my analyzation ryt?


    coz im a bit confuse on my first look on your code...

    i thought it's just an ordinary sequence of switch... (because i dont see a 'break' statement)

    so i thought it will return all the next values after the character that i passed....


    sorry for bad english......

  14. #12
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Roman Numbers

    Basically what happens here is that if you pass in 'V' it will fall through down to the 'M' case and run that code, the same thing happens if you pass in either 'X', 'L', 'C' or 'D'.

    You need to take note that switch cases falls through to the next one unless the case has a call to break in it.

    		switch(roman[i]){
    				case 'I':
    					total += value(roman[i]); break; // We call break here to break out of the switch
    				case 'V': // No break so fall down to X
    				case 'X': // No break so fall down to L
    				case 'L': // No break so fall down to C
    				case 'C': // No break so fall down to D
    				case 'D': // No break so fall down to M
    				case 'M':
    					if(i != 0 && (value(roman[i-1]) < value(roman[i]))){
    						total += value(roman[i]) - value(roman[i-1]);
    						i--;
    					}else{
    						total += value(roman[i]);
    					}
    					break; // BREAK
    			}

    // Json

  15. #13
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Roman Numbers

    what do you mean by 'fall down' sir?

    does it mean it also returns value ? ryt?

    in this case... we are only asking for the value of 'V' ... so the value of the rest are not used....

    am i ryt sir?

  16. #14
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Roman Numbers

    The next case statement doesn't stop execution of the previous case statement. So, when he says "fall down", he wants all the code from V...M to all be the same.

    char c = 'a';
    switch(c)
    {
    case 'a':
         System.out.println("The char is definitely an a");
    case 'b':
         System.out.println("The char is either an a or  b");
    }

    This will print out:

    The char is definitely an a
    The char is either an a or b

    If we take out the first print statement,
    switch(c)
    {
    case 'a':
    case 'b':
         System.out.println("The char is either an a or  b");
    }

    It will only print out:

    The char is either an a or b

  17. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (December 9th, 2009)

  18. #15
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Roman Numbers

    pls check this part please
    public class RomanNumbers3 {
     
        private static BufferedReader br = new BufferedReader(new InputStreamReader(
                                           System.in));
     
        public static void main(String[] args) throws IOException {
     
            int rN = 0;
            int equivalent = 0;
            int[] value;
     
            char[] romanChar = {'I'};
     
            System.out.print("Enter a roman number: ");
            String romanNumber = br.readLine();
     
            value = new int[romanNumber.length()];
            romanChar = new char[romanNumber.length()];
     
            // get the values of each of the roman character
            for (int x = romanNumber.length() - 1; x >= 0; x--) {
     
                switch (romanNumber.charAt(x)) {
     
                    case 'I':
     
                        rN = 1;
                        break;
     
                    case 'V':
     
                        rN = 5;
                        break;
     
                    case 'X':
     
                        rN = 10;
                        break;
     
                    case 'L':
     
                        rN = 50;
                        break;
     
                    case 'C':
     
                        rN = 100;
                        break;
     
                    case 'D':
     
                        rN = 500;
                        break;
     
                    case 'M':
     
                        rN = 1000;
                        break;
     
                    default:
     
                        System.out.println("Invalid Roman Character Sequence");
                        break;
                }
     
                // IM HAVING SOME PROBLEM WITH CHECKING OF THE CHARACTERS
                for (int check = romanChar.length - 1; check >= 0; check--) {
     
                    if (romanNumber.charAt(x) != romanChar[0]) {
     
                        System.out.println("WRONG CHARACTER");
                    }
                }
     
                // assign the value according to the sequence of the characters
                value[x] = rN;
            }
     
            for (int n = romanNumber.length() - 1; n >= 0; n--) {
     
                // compute the values according to the sequence of roman characters
                if ((n != 0) && (value[n] > value[n - 1])) {
     
                    equivalent = equivalent + (value[n] - value[n - 1]);
                    n--;
                }
                else {
     
                    equivalent = equivalent + value[n];
                }
            }
     
            System.out.println(equivalent);
        }
    }

    if i entered 'I' it still displays "WRONG CHARACTER"..when the character in the romanChar[] array has a character 'I' in its index(0) why?,.,.... cant figure it out... please help

    what i want here now is to check if there is/ are any illegal character.. so i will display an output for that..
    Last edited by chronoz13; December 12th, 2009 at 02:23 AM.

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  2. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM