Serial connection of Processing and Arduino

Or try this code

byte x = 0;
int speakerPin = 8;
void setup()
{
  pinMode(speakerPin, OUTPUT);
  Serial.begin(9600);
  
}
 
void loop() {

  if (int(x) == 1) {
    Do something 
  }
  else if (int(x) == 2) {
    Do something 
    
  }
  
  }
 
 
void serialEvent() {
  while (Serial.available()) {
    x = Serial.read();
  } 
}