Return String values like a list (line brake)

edited September 2014 in Questions about Code

Hey there,

I've got another quick question. I made this code, which can transfer multiple arab numbers into roman numbers. How can I "return" every roman number with a break? So that they won't clip together like they do now.

void setup() {
  int[] arabicNumber = new int[3];
  arabicNumber[1] = 555;
  arabicNumber[2] = 888;

  int numbers = arabicNumber.length;
  //println(numbers);

  size(200, 200);
  String romanNumber = "";

  getRomanNumber(arabicNumber, numbers);
} // Ende Setup

String getRomanNumber(int[] arabicNumber, int numbers) {

  String romanNumber = "";

  for (int i = 0; i < numbers; i++) {

    println(i);

  if (arabicNumber[i] >= 1000) {
    print ("Bitte geben Sie nur Werte bis 999 ein");
  }

  while (arabicNumber[i] >= 500) { 
    romanNumber += "D";
    arabicNumber[i] -= 500;
  }
  while (arabicNumber[i] >= 100) { 
    romanNumber += "C";
    arabicNumber[i] -= 100;
  }
  while (arabicNumber[i] >= 50) { 
    romanNumber += "L";
    arabicNumber[i] -= 50;
  }
  while (arabicNumber[i] >= 10) { 
    romanNumber += "X";
    arabicNumber[i] -= 10;
  }
  while (arabicNumber[i] >= 5) { 
    romanNumber += "V";
    arabicNumber[i] -= 5;
  }
  while (arabicNumber[i] >= 1) { 
    romanNumber += "I";
    arabicNumber[i] -= 1;
  }
  }

  romanNumber = romanNumber.replace("DCCCC", "CM"); //Siehe Tabellen auf http://www.roemische-zahlen.net/
  romanNumber = romanNumber.replace("CCCC", "CD");
  romanNumber = romanNumber.replace("LXXXX", "XC");
  romanNumber = romanNumber.replace("XXXX", "XL");
  romanNumber = romanNumber.replace("VIIII", "IX");
  romanNumber = romanNumber.replace("IIII", "IV");

  fill (#EA0E49);
  text (romanNumber, 30, 30);

  return romanNumber;
}

Best Regards, iSpectra

Answers

  • Methinks getRomanNumber() shoulda focused on 1 conversion at a time. ~:>

  • As said, you should separate concerns:

    • The function should convert only one number.
    • So, the loop should be in setup() and should call getRomanNumber() as many times as necessary.
    • And the function should not try to display the number: let the caller do that.
  • Ok, I understand. But how can I let the caller display the returned value?

  • Using text(), as you did, with the returned value, after the call...

  • Right, OMG ROFL. How did I miss that!? Thank you very much!

  • edited September 2014

    Anyways, my tweaked version (w/ negative roman numbers & 3999 limit!) I was doing a while ago: L-)

    /** 
     * ArabicToRoman (v2.23)
     * by  iSpectra (2013/Nov)
     * mod GoToLoop
     * 
     * forum.processing.org/two/discussion/1431/
     * return-string-values-like-a-list-line-brake
     *
     * www.roemische-zahlen.net/
     */
    
    void setup() {
      int[] values = {
        555, 888, 0, 4, -54, 999, -1000, 1003, -4000, 3999, 2501
      };
    
      for (int num: values)  println(num + "\t" + arabicToRomanNum(num));
      exit();
    }
    
    static final String arabicToRomanNum(int arabic) {
      String roman = "";
    
      if (arabic < 0) {
        roman = "-";
        arabic = abs(arabic);
      }
    
      if (arabic >= 4000)   return "Overflow!";
    
      while (arabic >= 1000) {
        roman += "M";
        arabic -= 1000;
      }
    
      while (arabic >= 500) {
        roman += "D";
        arabic -= 500;
      }
    
      while (arabic >= 100) {
        roman += "C";
        arabic -= 100;
      }
    
      while (arabic >= 50) {
        roman += "L";
        arabic -= 50;
      }
    
      while (arabic >= 10) {
        roman += "X";
        arabic -= 10;
      }
    
      while (arabic >= 5) {
        roman += "V";
        arabic -= 5;
      }
    
      while (arabic-- >= 1)  roman += "I";
    
      return roman.replace("DCCCC", "CM")
        .replace("CCCC", "CD")
          .replace("LXXXX", "XC")
            .replace("XXXX", "XL")
              .replace("VIIII", "IX")
                .replace("IIII", "IV");
    }
    
  • edited September 2014
    # ArabicToRoman (v2.23)
    # by  iSpectra (2013/Nov)
    # mod GoToLoop
    #
    # forum.processing.org/two/discussion/1431/
    # return-string-values-like-a-list-line-brake
    #
    # www.roemische-zahlen.net/
    
    
    setup: ->
    
      values = [555, 888, 0, 4, -54, 999, -1000, 1003, -4000, 3999, 2501]
      println num + '  =  ' + arabicToRomanNum num  for num in values
      do exit
    
    
    arabicToRomanNum = (arabic) ->
    
      roman = ''
      if arabic < 0 then roman = '-'; arabic = abs arabic
    
      return 'Overflow!'  if arabic >= 4000
    
      while arabic >= 1000 then roman += 'M'; arabic -= 1000
      while arabic >= 500  then roman += 'D'; arabic -= 500
      while arabic >= 100  then roman += 'C'; arabic -= 100
      while arabic >= 50   then roman += 'L'; arabic -= 50
      while arabic >= 10   then roman += 'X'; arabic -= 10
      while arabic >= 5    then roman += 'V'; arabic -= 5
    
      roman += 'I'  while arabic-- >= 1
    
      roman
        .replace('DCCCC', 'CM')
          .replace('CCCC', 'CD')
            .replace('LXXXX', 'XC')
              .replace('XXXX', 'XL')
                .replace('VIIII', 'IX')
                  .replace('IIII', 'IV')
    
Sign In or Register to comment.