Comparing two strings

Hello processing community. I am currently working on a project that listens for keyboard inputs and looks for combos of three keys pressed in succession (think Mortal Comabat). Here I want to trigger a red background when the left, up and right keys are pressed one after the other. keyPressed stores the pressed key (keycode) in the movelist array. After three presses, the first value is overwritten and so forth. Then the next part offsets each element by one, so that it will recognize the combo even if it was pressed over two series of three presses (is that clear?). BUT when I compare say order1 to combo1, nothing happens. Both output 373839 trough the println function so what is going on?

Thanks!

String[] movelist;         //my array containing my last three presses
String order1 = "null"; // to prevent null exception, you have a better way?
String order2 = "null";
String order3 = "null";

int i=0;

String combo1 = "373839"; // should equal keycode left, up and right

void setup() {

  movelist = new String[3]; 
}

void draw() {
}


void keyPressed() {

  if (i<=2) {

    movelist[i] = str(keyCode); //stores the keys in my array (position 0) as a string


    String order1 = movelist[0]+movelist[1]+movelist[2]; //offsets the position of the presses
    String order2 = movelist[1]+movelist[2]+movelist[0];
    String order3 = movelist[2]+movelist[0]+movelist[1];

    println(combo1); // 373839
    println(order1);  // equals 373839 after I press left up right
    i=i+1;                //shifts the record position by 1

  } else {

    i=0; // rewrite first entry after three presses

    movelist[i] = str(keyCode);

    String order1 = movelist[0]+movelist[1]+movelist[2];
    String order2 = movelist[1]+movelist[2]+movelist[0];
    String order3 = movelist[2]+movelist[0]+movelist[1];


    println(combo1); // 373839
    println(order1);   // equals 373839 after I press left up right
    i=i+1;
  }

  if (combo1.equals(order1)==true) { //for some reason this doesn't work! combo1.equals("373839") works. 
    background(255, 0, 0);                  //order1.equals("373839") does NOT work. why?
  } else {
    background(0, 255, 0);
  }
}

Answers

  • edited April 2017

    The problem is that you are creating multiple variables for order1, order2 and order3.

    You have the global values which are initialised to null then two sets of local variables 26-28 which are local to the if statement and variables 40-42 which are local to the else part

    Simply remove the data type identifier String from these lines then they will all use the global ones.

  • edited April 2017

    Also the algorithm used in keyPressed is much more complex that it needs to be try this out

    String[] movelist;         //my array containing my last three presses
    String order1 = "null"; // to prevent null exception, you have a better way?
    String order2 = "null";
    String order3 = "null";
    
    int i=0;
    
    String combo1 = "373839"; // should equal keycode left, up and right
    
    void setup() {
      movelist = new String[3];
    }
    
    void draw() {
    }
    
    
    void keyPressed() {
    
      movelist[i] = str(keyCode); //stores the keys in my array (position 0) as a string
    
      order1 = movelist[0]+movelist[1]+movelist[2]; //offsets the position of the presses
      order2 = movelist[1]+movelist[2]+movelist[0];
      order3 = movelist[2]+movelist[0]+movelist[1];
    
      i++;    //shifts the record position by 1
      i %= 3;  // wrap the record postion so it goes 0 1 2 0 1 2 0 1 ... etc.
    
      println(combo1, order1, (combo1.equals(order1))); // 373839
    
      if (combo1.equals(order1)==true) { //for some reason this doesn't work! combo1.equals("373839") works. 
        background(255, 0, 0);                  //order1.equals("373839") does NOT work. why?
      } else {
        background(0, 255, 0);
      }
    }
    
  • @quark Hummm. WOW. That is exactly where I came here, to learn from the best. I didn't know how to implement the % but I didn't quite know how. Many thanks!

Sign In or Register to comment.