How to randomly fluctuate between lowercase, uppercase, and originally formatted text formatting?

edited November 2016 in Questions about Code

This is part of a larger project, but I've been trying to put lowercase, uppercase, and "" in its own string and combine it with the string text title, but it just puts it at the end of text

String headLine="text1";
String body="text2";
String details="text3";

String[] cases= {".toLowerCase()", ".toUpperCase()", ""};

int index = int(random(cases.length));
    text (body + cases[index], int(random(50,300)), int(random(0,600)), int(random(400,700)), height);

Answers

  • Answer ✓

    What you have in that array is a list of names of methods, you can't apply them dynamically like that, Java doesn't do that.

    I would pick a random number from 1 to 3 and use this in a switch statement, calling the methods in the relevant case statement.

  • edited November 2016 Answer ✓
    final int ORIGINAL = 0, LOWER = 1, UPPER = 2;
    final String headLine = "Text1", body = "Text2", details = "Text3";
    
    String txt = body;
    
    switch ((int) random(3)) {
    case LOWER:
      txt = body.toLowerCase();
      break;
    
    case UPPER:
      txt = body.toUpperCase();
    }
    
    println(txt);
    exit();
    
  • Wow in my combing of the reference library didn't even see switches. Thanks!

  • @gmunz --

    To extend @GoToLoop 's solution slightly, if this was a function, how would you extend it to also randomly return titlecase? (Titlecase Is Written Like This).

    final int ORIGINAL = 0, LOWER = 1, UPPER = 2;
    final String sampleText = "LOREM Ipsum sit dolor.";
    
    void setup(){
      for (int i=0; i<5; i++) {
        println(randomCase(sampleText));
      }
    }
    
    String randomCase(String str) {  
      switch ((int) random(4)) {
      // case ORIGINAL:
      //   break;
      case LOWER:
        str = str.toLowerCase();
        break;
      // case TITLE:
        // how would you convert a string to title case?
      case UPPER:
        str = str.toUpperCase();
      }
      return str;
    }
    

    Unlike toLowerCase() and toUpperCase(), which are built-in, in order to implement titlecase you might need to manually loop over the characters in the string, check if the character to the left is a letter or not, and capitalize them if not.

  • edited November 2016

    Since P3, the pre-compiler supports enum keyword for ".pde" tabs too. \m/
    I think this case is a gr8 opportunity to toy w/ it: :bz

    /**
     * Enum String Cases (v1.0.1)
     * GoToloop (2016-Nov-14)
     *
     * https://forum.Processing.org/two/discussion/19014/
     * how-to-randomly-fluctuate-between-lowercase-uppercase-
     * and-originally-formatted-text-formatting#Item_5
     */
    
    static final String SAMPLE = "LOREM IpsUm sit doloR.";
    
    void setup() {
      for (int i = 0; i < 5; ++i) {
        final int idx = (int) random(Case.length);
        final Case mode = Case.array[idx];
        final String txt = mode.change(SAMPLE);
        println(mode + ":\t" + txt);
      }
    
      exit();
    }
    
    enum Case {
      ORIG {
        @Override String change(final String s) {
          return s;
        }
      },
    
      LOWER {
        @Override String change(final String s) {
          return s.toLowerCase();
        }
      },
    
      UPPER {
        @Override String change(final String s) {
          return s.toUpperCase();
        }
      },
    
      TITLE { // http://StackOverflow.com/a/1086134
        @Override String change(final String s) {
          final StringBuilder titled = new StringBuilder(s);
          final int len = titled.length();
          boolean nextCharIsTitle = true;
    
          for (int i = 0; i < len; ++i) {
            final char ch = titled.charAt(i);
    
            if (Character.isSpaceChar(ch))  nextCharIsTitle = true;
            else if (nextCharIsTitle) {
              nextCharIsTitle = false;
              titled.setCharAt(i, Character.toTitleCase(ch));
            }
          }
    
          return titled.toString();
        }
      };
    
      static final Case[] array = Case.values();
      static final int length = array.length;
    
      abstract String change(final String s);
    }
    
  • edited November 2016

    @GoToLoop -- very cool -- I didn't know enums were allowed, and nice StringBuilder!

    (Forum needs @ Override to be cheated to make the code readable.)

  • edited November 2016

    Forum needs @Override to be cheated to make the code readable.

    DONE! >:)

  • edited November 2016

    ... and nice StringBuilder!

    Actually my Case.TITLE.change() was based on this sample code: :-\"
    http://StackOverflow.com/a/1086134

    Although I've tweaked it a little. :ar!

    His relies on append(). While mine goes w/ setCharAt():
    http://docs.Oracle.com/javase/8/docs/api/java/lang/StringBuffer.html

  • edited November 2016

    Added a static method to enum Case called randomCase(): O:-)

    /**
     * Enum String Cases (v2.2.1)
     * GoToloop (2016-Nov-14)
     *
     * https://forum.Processing.org/two/discussion/19014/
     * how-to-randomly-fluctuate-between-lowercase-uppercase-
     * and-originally-formatted-text-formatting#Item_9
     */
    
    static final String SAMPLE = "LOREM IpsUm sit doloR.";
    
    void setup() {
      for (int i = 0; i < 5; ++i) {
        final Case mode = Case.randomCase(this);
        final String txt = mode.change(SAMPLE);
        println(mode + ":\t" + txt);
      }
    
      exit();
    }
    
    enum Case {
      ORIG {
        @Override String change(final String s) {
          return s;
        }
      },
    
      LOWER {
        @Override String change(final String s) {
          return s.toLowerCase();
        }
      },
    
      UPPER {
        @Override String change(final String s) {
          return s.toUpperCase();
        }
      },
    
      TITLE { // http://StackOverflow.com/a/1086134
        @Override String change(final String s) {
          final StringBuilder titled = new StringBuilder(s);
          final int len = titled.length();
          boolean nextCharIsTitle = true;
    
          for (int i = 0; i < len; ++i) {
            final char ch = titled.charAt(i);
    
            if (Character.isSpaceChar(ch))  nextCharIsTitle = true;
            else if (nextCharIsTitle) {
              nextCharIsTitle = false;
              titled.setCharAt(i, Character.toTitleCase(ch));
            }
          }
    
          return titled.toString();
        }
      };
    
      static final Case[] array = Case.values();
      static final int length = array.length;
    
      static final Case randomCase() {
        final int idx = (int) (Math.random() * length);
        return array[idx];
      }
    
      static final Case randomCase(final PApplet p) {
        final int idx = (int) p.random(length);
        return array[idx];
      }
    
      abstract String change(final String s);
    }
    
Sign In or Register to comment.