Comparing two Strings with equals()
in
Programming Questions
•
2 years ago
I can't seem to get str.equals(str) working correctly.
I have a string that looks like this: 02:12#0#1.0668 where the number between the two # # signs is either 0 or 1. I want to output 'low tide' for 0 and 'high tide' for 1.
Here's what reference has to say about equals():
Version 1, attempted failsafe:
public void ShowTides() {
String[] t1 = splitTokens(Tide1,"#");
String hl = t1[1]; String ht = "1"; String lt = "0";
fill(c_text);
if (hl.equals(ht) == true) {hl = "Low Tide"};
if (hl.equals(lt) == true) {hl = "High Tide"};
text(t1[0] +" am ",x,y);
}
Version 2, what I thought would work:
public void ShowTides() {
String[] t1 = splitTokens(Tide1,"#"); // 02:12#0#1.0668
fill(c_text);
if (t1[1].equals("1") == true) {hl = "Low Tide"};
if (t1[1].equals("0") == true) {hl = "High Tide"};
text(t1[0] +" am ",x,y);
}
In version 1 above, I thought if I set a variable to t1[1] and used that instead of t1[1] it would work but apparently its something else. It's got me stumped but it's probably something very simple.
I have a string that looks like this: 02:12#0#1.0668 where the number between the two # # signs is either 0 or 1. I want to output 'low tide' for 0 and 'high tide' for 1.
Here's what reference has to say about equals():
String str1 = "CCCP";I've tried the following bits of code, but in each case, it won't compile unless I comment out the two lines containing the equals() function.
String str2 = "CCCP";
// Tests to see if str1 is equal to str2
if(str1.equals(str2) == true) {
println("Equal"); // They are equal so this line will print
} else {
println("Not equal");
}
Version 1, attempted failsafe:
public void ShowTides() {
String[] t1 = splitTokens(Tide1,"#");
String hl = t1[1]; String ht = "1"; String lt = "0";
fill(c_text);
if (hl.equals(ht) == true) {hl = "Low Tide"};
if (hl.equals(lt) == true) {hl = "High Tide"};
text(t1[0] +" am ",x,y);
}
Version 2, what I thought would work:
public void ShowTides() {
String[] t1 = splitTokens(Tide1,"#"); // 02:12#0#1.0668
fill(c_text);
if (t1[1].equals("1") == true) {hl = "Low Tide"};
if (t1[1].equals("0") == true) {hl = "High Tide"};
text(t1[0] +" am ",x,y);
}
In version 1 above, I thought if I set a variable to t1[1] and used that instead of t1[1] it would work but apparently its something else. It's got me stumped but it's probably something very simple.
2