Remove different amounts of text from two strings based on delimiters?

When the string -english- contains #,$,%,&, or q, and you press backspace, one character is to be deleted from -english-, and 2 from -tenglish-. All other times when you press backspace, one character is removed from both. However, this code deletes a massive chunk of text on the first press, then almost (but not quite) does what it's supposed to. I've been pouring over it for a few minutes, anyone willing to give me a quick debug?

String english = "";
String tenglish = "";
PFont f;

void setup() {
  size(500, 650);
  f = loadFont("ArialMT-30.vlw");
  textFont(f, 12);
}

void draw() {
  background(255);
  fill(0);
  text(english, 0, height-30);
  text(tenglish, 0, height-100);
}

void keyReleased() {
  if (key!=BACKSPACE&&key!=CODED&&key!=DELETE&&key!=ENTER) {
    english+=key;
    tenglish+=key;
  }
  transliterate();
}

void keyPressed() {
  if (key ==BACKSPACE&&english.length()>0) {
    if (english.charAt(english.length()-1)== '#' && english.charAt(english.length()-1)== ' && english.charAt(english.length()-1)== '%' && english.charAt(english.length()-1)== '&' && english.charAt(english.length()-1)== 'q') {
      tenglish = tenglish.substring(0, english.length()-2);
    }
    else {
      tenglish =  tenglish.substring(0, english.length()-1);
    }
    english = english.substring(0, english.length()-1);
  }
}

void transliterate() {
  english=english.toLowerCase();
  english=join(split(english, "ch"), "#");
  english=join(split(english, "sh"), "$");
  english=join(split(english, "th"), "%");
  english=join(split(english, "ng"), "&");
  english=join(split(english, "qu"), "q");
}

Answers

  • This isn't what I need, sorry. Unless you can show me how to apply it?

  • edited March 2014

    Well, I found one problem:

    This line (28-30): if (english.charAt(english.length()-1)== '#' && english.charAt(english.length()-1)== ' && english.charAt(english.length()-1)== '%' && english.charAt(english.length()-1)== '&' && english.charAt(english.length()-1)== 'q') { tenglish = tenglish.substring(0, english.length()-2); }

    should have ||'s instead of &&'s.

  • The other problem is that I reformat the wrong strings on lines 29 and 32. Problem solved!

  • Answer ✓

    I think

    This line (28-30):

    if (english.charAt(english.length()-1)== '#' && english.charAt(english.length()-1)== ' && english.charAt(english.length()-1)== '%' && english.charAt(english.length()-1)== '&' && english.charAt(english.length()-1)== 'q') { tenglish = tenglish.substring(0, english.length()-2); }
    

    can be written as

    signs = "#$%&q";
    if (signs.indexOf( english.charAt(english.length()-1) ) > -1)  { 
        tenglish = tenglish.substring(0, english.length()-2); }
    

    example:

    String english="hello#";
    println (english);
    String signs = "#$%&q";
    if (signs.indexOf(english.charAt(english.length()-1)) > -1) { 
      println ("yes");
      english = english.substring(0, english.length()-1);
    }
    println (english);
    

    (one $ sign in your code is missing the forum killed it ?)

  • or even:

    void setup() {
      String english="hello#";
      println (english);
      String signs = "#$%&q";
      char letter = english.charAt(english.length()-1); 
      if (signs.indexOf(letter) > -1) { 
        println ("yes");
        english = shorten (english);
      }
      println (english);
    }
    
    String shorten(String str1) {
      if (str1==null) return null;
      if (str1.equals("")) return null;
    
      return str1.substring(0, str1.length()-1);
    }
    //
    
  • Wound up doing this:

    String shorten(String str1, int shortenedBy) {
      if (str1==null) return null;
      if (str1.equals("")) return null;
    
      return str1.substring(0, str1.length()-shortenedBy);
    }
    
Sign In or Register to comment.