Want to convert string to int

Hello, I’m trying to make a program to basically calculate COVID risk off the user’s age. Right now, I coded for the user to be able to type their age, but I want the string they type to be converted to an int when they press enter.

PFont font;
String typedAge = "";
int userAge;
void setup(){
  size(300,300);
  font = createFont("Arial",20);
}

void draw(){
  background(255);
  println(userAge);
  textFont(font);
  fill(0);
  textAlign(CENTER);
  text("Type in Age", width/2, height/2);
  text("Click Enter to Save", width/2, height/2 + 25);
  text("Age:  " + typedAge, width/2 - 25, height/2 + 50);
}

void keyPressed(){
  if(key == '\n'){
   // this is where the conversion would happen, saving typedAge to userAge
  } else{
    typedAge = typedAge + key;
  }
}

Nevermind, I used userAge = int(typedAge);, worked well.

3 Likes