Results 1 to 12 of 12
Like Tree8Likes
  • 1 Post By Zelaine
  • 4 Post By Zelaine
  • 2 Post By KevinWorkman
  • 1 Post By Junky

Thread: Roman Numerals

  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Post Roman Numerals

    I have tried to code a program that lets you enter an integer, and then converts it to a Roman numeral. But it didn't work out that well, some numbers, like 10, 30 or 500, for some reason get really messed up, but some numbers work perfectly. If I for instance enter 10, I get "XIXI", which clearly is wrong. Could you guys take a look at my code and try to find the problem? Because that would be really helpful :)

    Thanks in advance!

    Java Code:
    import java.util.Scanner;
    import java.util.Vector;
    
    public class Test{
    	public static void main(String[] args){
    		Scanner hej = new Scanner(System.in);
    		Vector<String> romanNumerals = new Vector<String>();
    		romanNumerals.add("M"); romanNumerals.add("CM"); romanNumerals.add("D"); romanNumerals.add("CD"); romanNumerals.add("C");
    		romanNumerals.add("XC"); romanNumerals.add("L"); romanNumerals.add("XL"); romanNumerals.add("X"); romanNumerals.add("IX");
    		romanNumerals.add("V"); romanNumerals.add("IV"); romanNumerals.add("I");
    		int numberArray[] = new int[13];
    		numberArray[0] = 1000; numberArray[1] = 900; numberArray[2] = 500; numberArray[3] = 400; numberArray[4] = 100;
    		numberArray[5] = 90; numberArray[6] = 50; numberArray[7] = 40; numberArray[8] = 10; numberArray[9] = 9;
    		numberArray[10] = 5; numberArray[11] = 4; numberArray[12] = 1;
    		String result = "";
    		System.out.print("Enter an integer from 1 to 1000: ");
    		int number = hej.nextInt();
    		for(int x=0;x<romanNumerals.size();x++){
    			if(x != 4 && x != 8 && x != 12){
    				if(number/numberArray[x]>=1){
    					result += romanNumerals.get(x);
    					number %= numberArray[x];
    				}
    			}else{
    				if(number/numberArray[x]>=1){
    					double test = number;
    					if(test >= 100)
    						test /= 100;
    					else if(test >= 10)
    						test /= 10;
    					for(int y=0;y<test;y++){
    						result += romanNumerals.get(x);
    					}
    					test %= numberArray[x];
    				}
    			}
    		}
    		System.out.println(result);
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default Re: Roman Numerals

    Have you stepped through this with a debugger, or at least added some print statements, to help figure out what's going on?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Roman Numerals

    Thanks! I did what you said and found the problem :) On line 34 I wrote
    Java Code:
    number %= numberArray[x];
    instead of
    Java Code:
    test %= numberArray[x];
    KevinWorkman likes this.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Roman Numerals

    Good job.

    Note that you can save a bunch of ugly initialization code by initializing the array like this:

    Java Code:
    int numberArray[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Roman Numerals

    Thanks for the tip! :) Is there a similar way for the
    Java Code:
    Vector<String>
    type?

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Roman Numerals

    Java Code:
    romanNumerals.addAll(Arrays.asList("M", "CM", "D", .....));
    Regards,
    Jim
    Last edited by jim829; 09-10-2013 at 07:07 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Roman Numerals

    Thanks Jim! :)
    But your suggestion didn't work :(
    The debugger just says "Arrays cannot be resolved".
    Last edited by Zelaine; 09-10-2013 at 09:02 PM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Roman Numerals

    You have to import the Arrays class. I'll leave it as an exercise for you to check the API and find what package it's in.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Roman Numerals

    Thanks a lot guys! It works now :) You're so nice for helping me out with this, I mean really really nice

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default Re: Roman Numerals

    Pretty refreshing to see somebody willing to put in the work and investigate our advice. Nice work OP, you've temporarily restored my faith in humanity.
    jim829 and Huskies like this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Roman Numerals

    Yeah. And the OP even thanked us.

    Regards,
    Jim
    Last edited by jim829; 09-11-2013 at 03:29 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Roman Numerals

    <Fawlty Towers>
    A happy customer. We should have them stuffed!
    </Fawlty Towers>
    DarrylBurke likes this.

Similar Threads

  1. Sorting roman numerals
    By maria in forum New To Java
    Replies: 2
    Last Post: 03-10-2013, 09:12 PM
  2. Convert the number to Roman numerals
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 9
    Last Post: 11-29-2012, 10:44 PM
  3. Roman Numerals help
    By umaza in forum New To Java
    Replies: 2
    Last Post: 05-22-2012, 01:02 AM
  4. Convert roman numerals
    By matzahboy in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 10:06 PM
  5. Convert roman numerals to integers
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 11:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •