# Is there a better way to compare multiple Strings?

**URL:** <https://discourse.processing.org/t/is-there-a-better-way-to-compare-multiple-strings/32024>\
**Category:** Coding Questions\
**Created:** [August 31, 2021, 11:34am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-compare-multiple-strings/32024 "2021-08-31T11:34:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaotic](https://avatars.discourse-cdn.com/v4/letter/c/c5a1d2/32.png) [@chaotic](https://discourse.processing.org/u/chaotic)\
**Post date:** [August 31, 2021, 11:34am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-compare-multiple-strings/32024/1 "2021-08-31T11:34:34Z")

</div>

I’m trying to write a code that will only allow you to enter certain numbers. I found a way but I feel like there should be a more efficient way.

```auto
String text="";
String saved="";
String degree90="90";
String degree89="89";
void setup(){
  size(700,700);
  }
void draw(){
  background(200);
  fill(180);
  rect(325,268,50,40);
  textAlign(CENTER);
  textSize(35);
  fill(0,0,255);
  text("\nHit enter to save",350,200);
  text(text,350,300);
  text(saved,350,400);
}
void keyPressed(){
  if (key==BACKSPACE) {
    if (text.length()>0) {
      text=text.substring(0, text.length()-1);
    }
  }
  if (key == '\n'){
    if((text.equals(degree90))||(text.equals(degree89))){
      saved=text;
      text="";
    }
  }else{
    text=text+key;
  }
}

```

I feel like “if((text.equals(degree90))||(text.equals(degree89))” can be improved since I want to do from degree0 to degree90. Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [September 1, 2021, 7:44am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-compare-multiple-strings/32024/2 "2021-09-01T07:44:28Z")

</div>

Hi @chaotic!

Welcome to the forum!  
Is there any specific sequence of numbers?  
You could try regex to get the numbers or depending on the input you could try to convert the string into an integer to get the number and check if it is either inside an interval or present in an array of expected values. Examples on how to convert the string into integer are below:

```auto
String a = "90";
int b = Integer.parseInt(a);
println("B=" + b);

String c = "degree90";
int d = Integer.parseInt(c.replaceAll("[\\D]", ""));
println("D=" + d);

```

Hope it helps!  
Best regards

---

<div class="post-metadata">

**Author:** ![chaotic](https://avatars.discourse-cdn.com/v4/letter/c/c5a1d2/32.png) [@chaotic](https://discourse.processing.org/u/chaotic)\
**Post date:** [September 1, 2021, 5:40pm UTC](https://discourse.processing.org/t/is-there-a-better-way-to-compare-multiple-strings/32024/3 "2021-09-01T17:40:51Z")

</div>

What I’m trying to do is allow the user to input a value between 0 and 90. If they do it will save it.

---

<div class="post-metadata">

**Author:** ![chaotic](https://avatars.discourse-cdn.com/v4/letter/c/c5a1d2/32.png) [@chaotic](https://discourse.processing.org/u/chaotic)\
**Post date:** [September 1, 2021, 7:01pm UTC](https://discourse.processing.org/t/is-there-a-better-way-to-compare-multiple-strings/32024/4 "2021-09-01T19:01:49Z")

</div>

I figure it out!

```auto
String text="";
String saved="";
int degree=0;

void setup(){
  size(700,700);  
}

void draw(){
  degree=int(0+text);
  background(200);
  fill(180);
  rect(325,268,50,40);
  textAlign(CENTER);
  textSize(35);
  fill(0,0,255);
  text("\nHit enter to save",350,200);
  text(text,350,300);
  text(saved,350,400); 
}

void keyPressed(){
  if (key==BACKSPACE) {
    if (text.length()>0) {
      text=text.substring(0, text.length()-1);
    }
  }
  if (key == '\n'){
    if((text.length()<=2)&&(degree>=0)&&(degree<=90)){
      saved=text;
      text="";
    }
  }else{
    text=text+key;
  }
  if (key==BACKSPACE) {
    if (text.length()>0) {
      text=text.substring(0, text.length()-1);
    }
  }
}

```

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 1, 2021, 7:17pm UTC](https://discourse.processing.org/t/is-there-a-better-way-to-compare-multiple-strings/32024/5 "2021-09-01T19:17:46Z")

</div>

> [@chaotic](#):
>
> `degree=int(0+text);`

Looks good! One suggestion – update `degree=int(0+text);` in keyPressed just after you update `text=text+key`. Otherwise it is possible that a new key+enter might both get processed by keypressed before degrees gets updated on the next frame, and then enter will be ignored even though the text value is correct.
