How to read 2 consecutive chars sent to arduino from computer?

My program reads the input and plays the appropriate note for 500 milliseconds. If I type in 'a', tone(8, 440, 500) will be called. I now want to add different note lengths so that in the future, I can play songs by sending code to Arduino. I was thinking that if 'a' is typed in, the program checks the next character. If it's a 1, it's a whole note. If it's a 2, it's a half note. My code doesn't work. What is wrong with it?

    if(Serial.available()>0){
    char character=Serial.read();
    if(character=='a'){
    char leng=Serial.read();
    if(leng=='1'){
    tone(8, 440, 1000);
    delay(1000);
    }
    else if(leng=='2'){
    tone(8, 440, 500);
    delay(500);
    }

    }
    }

Have a look at Serial Input Basics

...R

Turned out the arduino couldn't read the values fast enough. Adding while (!Serial.available()){} before the second read solved the problem. Thank you for the help

Turned out the arduino couldn't read the values fast enough

More likely that it was reading them faster than they were arriving.

Your code checked that at least one character was available then blindly read two without knowing that the second one had arrived.

First, take a careful read of the post that Robin suggested. Second, with your code in the source window in the IDE, simultaneously press the Ctrl and T keys (Ctrl-T) and see what happens. It will make life easier on those who have to read your code. Third, what happens if the user enters an 'A' instead of 'a'? Fourth, read up on the toupper() and tolower() macros and see if either of those might be of help.