Results 1 to 12 of 12
  1. #1
    tabako is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default Arabic to roman and Roman to Arabic Converter Program

    I am a beginner at Java and for class we have to write a roman to Arabic converter and Arabic to roman numeral converter. However, we cannot use arrays, methods, or loops. I guess I just need a high level of how you would start this project using if else statements.

    My code is getting the user input and I have all that down. But how would you approach the tests for a roman numeral such as MCMXCV and return 1995. I am just stuck and looking for someone to get me a start so that I understand the logic behind it before coding it.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    If you can't use methods, you can't make anything in Java. It's that simple. Are you sure you didn't mishear your professor?

  3. #3
    tabako is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Yes I'm positive lol, i can give you the link here...

    http://www.cs.uwec.edu/~stevende/cs1...rams/roman.pdf

    Somewhere in there it says "When converting the number you are not to use loops, arrays or methods. The focus of this assignment is on selection statements."

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Note for the future; don't leave assignments 'till the last day. 'Cause right now I'm really confused as to how you're supposed to make a program with no methods.

    A method is synonym for a function; a series of commands that takes a list of parameters and does or does not return something for the calling code.
    Java Code:
    public void a(int b) { return b; }
    ...that kind of thing.

    I'm also unsure how he expects this to be done without loops. Imagine you enter 17,000. That would be MMMM...MM (17 times). In your assignment he says to do the max as 3,999, but even still, that's a ridiculous idea.

    Ignoring all that, we can just assume everything is your main method, as that's where your input stuff is. You will want to use a series of if/elseif/else statements to determine your value.

    Remember the series of numbers in Roman... M = 1000, so first, you should check if it's greater than 1000, then if it is, add M to your string and subtract 1000. You can do this for every letter as many times as necessary, and that should get you a pretty accurate representation of what you need.

    As I said, I'm not sure how elegant that will be without loops... but it should at least get you started.

    Since the focus of your assignment is selection statements, have a look here:
    if Selection Statements in Java - Java Now

    Good luck! ;)

  5. #5
    tabako is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    so i get what you are saying but what i don't get is how to get the number after the "M" or whatever number you input.
    int year = 0;

    So my code looks like this:


    int arabic = 0;
    String arabic = JOptionPane.showInputDialog(null, "Enter a Arabic number");

    year = Integer.parseInt(arabic);



    if(year >= 1000){
    JOptionPane.showMessageDialog(null, "M" + (year - 1000));
    }
    else if(year >= 500){
    JOptionPane.showMessageDialog(null, "D" + (year - 500));
    }
    else if(year >=100){
    JOptionPane.showMessageDialog(null, "C" + (year - 100));
    }

    etc...


    so if you type something in like 110 it'll come back as C10 instead of CX, so i was wondering for the expressions for (year - 1000) etc. how would you change those to the correct representation of those into roman numerals.

    Maybe what i don't understand is what you mean by "add M to your string and subtract 1000" because im not really adding those values to the string?
    Last edited by tabako; 10-08-2010 at 06:13 AM.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Instead of doing it by showing the message dialog then quitting the statements, you want to chain them together and keep a string record of what your value is.

    You know, something like this:
    Java Code:
    String x = "";
    x += "M";
    x += "C"; // now x == "MC"
    Then after all your if statements, you can display the value of x. (Though, name your variable something better than x.)

    Also, you will want to keep track of the year and manipulate it as you go. Once you add the M in your year >= 1000 statement, you have to decrement the year by 1000. Otherwise you're just reading a year (2010 for example) over and over--instead of it being 2010, then 1010, then 10, then 0.

    If you're having troubles with tracking the year, break it down as I just did. Try it with a few numbers and you'll see how it needs to be broken down.

  7. #7
    tabako is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    i feel like im doing this sort of wrong, however i do believe that it is something similar to what you wrote down. I feel like im going to have to wayyyyy to many if statements, so just to clarify:

    (btw i used x, y, and z for variables for now since i don't really care that much)

    Code:
    String x = "";
    String y = "";
    String z = "";
    String ab = "";
    String ac = "";
    if(year >= 3000){
    x+="MMM";
    }
    else if(year >= 2000){
    x+="MM";
    }
    else if(year >= 1000){
    x+="M";
    }
    else if(year >= 500){
    x+="D";
    }
    else if(year >= 100){
    x+="C";
    }
    else if(year >= 50){
    x+="L";
    }
    else if(year >= 10){
    x+="X";
    }
    else if(year >= 5){
    x+="V";
    }
    else if(year >= 1){
    x+="I";
    }

    if((year > 3000) && (year > 3500)){
    y+= "D";
    }
    else if((year > 3000) && (year > 3100 )){
    y+= "C";
    }
    else if((year > 3000) && (year > 3050)){
    y+= "L"
    }

    Like i don't want to go through all of this and be done with it and know that i accomplished nothing, so i still don't know if im going at this problem the right way lol. I usually don't like asking this many questions cause then i feel like im just getting "carried" through the HW. but i do appreciate all of the help
    Last edited by tabako; 10-08-2010 at 07:23 AM.

  8. #8
    tabako is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    It also seems difficult to do if and else statements when the number is something like 3400 where the roman answer would be MMMCD instead of MMMCCCC so yeah..

  9. #9
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Your amount of if statements (at least for the x variable) is natural. You need one for each of the Roman numeric letters.

    Honestly though here you probably don't want else in between those. That means if you have 110, it will hit the 100, then skip the 10. (I know I said to use if/elseif/else statements but that's just the general format for selections; in this case it's just a string of ifs.)

    Not sure what you're after with the y variable, though.

    And you also forgot to subtract the amount from your year as you go--remember, 2010, then 1010, then 10, then 0.

    PS: @ Your latest reply, yes; Ithink in reality you have to check for 4 in a row of something then do a subtraction method. Alternately, you could do this:
    Java Code:
    if (year >= 900) {
        x += "CM";
        year -= 900;
    }
    ...then continue on with checking if it's >= 1000. That might be easier. ;) This gets a bit complex though as you will have issues when you have numbers > 1000 and are subtracting 900.

    If I were you, I'd work this algorithm out on paper before coding it. You will find it's a lot easier to visualize.
    Last edited by Zack; 10-08-2010 at 08:19 AM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    ceautery: Thanks for your help in this thread, but I've delete it as it is a complete solution. Let's try to avoid spoon-feeding and instead guide the original poster so we don't cheat him out of the benefit of creating a code solution for himself. Thanks for your cooperation.

  11. #11
    hetulhh is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

  12. #12
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Firstly, avoid just handing out solutions as the OP can just copy and paste and learn nothing.

    Second, this thread is almost a month old.

Similar Threads

  1. arabic encoding in url
    By echoe_echoe in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 10-06-2010, 09:10 AM
  2. Convert roman numerals
    By matzahboy in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 11:06 PM
  3. Roman numeral to Decimal
    By hector100 in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 08:48 PM
  4. Arabic Problem In Sony Ericsson
    By medhat_fci in forum Advanced Java
    Replies: 2
    Last Post: 09-20-2008, 04:20 PM
  5. Arabic text in JApplet
    By Bedoor in forum Advanced Java
    Replies: 0
    Last Post: 01-15-2008, 01:52 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
  •