Page 2 of 2 FirstFirst 12
Results 21 to 23 of 23
  1. #21
    cdawg_2010 is offline Member
    Join Date
    Oct 2010
    Posts
    11
    Rep Power
    0

    Default

    Heres an update on what I have currently...im still not sure on the last part with the printInt:

    public class Roman {

    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 = { "M", "CM", "D", "CD", "C", "XC",
    "L", "XL", "X", "IX", "V", "IV", "I" };


    public Roman(String x) {


    if (x.length() == 0)
    throw new NumberFormatException("There is no input!");

    x = x.toUpperCase();


    int m = 0;
    int arabic = 0;

    while (m < x.length()) {

    char letter = x.charAt(m);
    int number = letterToNumber(letter);

    if (number < 0)
    throw new NumberFormatException("Not a Roman numeral: " + letter );

    m++;

    if (m == x.length()) {
    arabic += number;
    }
    else {
    int nextNumber = letterToNumber(x.charAt(m));
    if (nextNumber > number) {

    arabic += (nextNumber - number);
    m++;
    }
    else {

    arabic += number;
    }
    }

    }
    }

    private int letterToNumber(char letter) {
    switch (letter) {
    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 -1;
    }
    }

    public String printRoman() {
    String x = "";
    int N = num;
    for (int m = 0; m < numbers.length; m++) {
    while (N >= numbers[m]) {
    x += letters[m];
    N -= numbers[m];
    }
    }
    return x;
    }


    public int printInt() {

    return num;
    }



    }

  2. #22
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Is it Just me? But i cannot read code with if statements with no braces.??

    i've tried but i can't figure out where they end.

    infact i didn't even know you could not use braces in if statements :S

  3. #23
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,229
    Rep Power
    19

    Default

    I know that 1986 would be MDCCCLXXXVI i think
    Think again. MDCCCLXXXVI is 1886.

    Hint: 900 in Roman is CM

Page 2 of 2 FirstFirst 12

Similar Threads

  1. problem in creating a class that should always run in the server
    By harsha.udupa2008 in forum New To Java
    Replies: 1
    Last Post: 07-14-2010, 02:56 PM
  2. Help creating a Date class
    By Meta in forum New To Java
    Replies: 17
    Last Post: 04-30-2010, 09:28 AM
  3. Replies: 2
    Last Post: 03-08-2010, 10:29 AM
  4. Creating a Defined Class with Member Functions
    By New2Java in forum New To Java
    Replies: 6
    Last Post: 08-05-2009, 09:05 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
  •