String a = "s";
String b = "Hellos";
System.out.println(a.toLowerCase().substring(a.length() - 1, a.length()));
System.out.println(b.toLowerCase().substring(b.length() - 1, b.length()));
if (a.toLowerCase().substring(a.length() - 1, a.length()) == b.toLowerCase().substring(b.length() - 1, b.length()))
System.out.println("true");
else
System.out.println("false");
I put the two System.out.println at the top to see what comes out. Here, they look like they're equal, but it comes out as "false." What's going on here? It doesn't matter if they're uppercase or lowercase, so I put .toLowerCase on them.
4 replies to this topic
#1
Posted 15 March 2011 - 09:56 PM
I'm supposed to compare the end of two strings and see if they are equal. Here's my code:
|
|
|
#2
Posted 15 March 2011 - 11:30 PM
They are false because you use ' == ' instead of ' .equals(..)'
' == ' will check if their addresses in the memory are equal. equals() checks their value.
Know that strings have an endsWith() method..
' == ' will check if their addresses in the memory are equal. equals() checks their value.
Know that strings have an endsWith() method..
#3
Posted 16 March 2011 - 03:59 PM
wim DC said:
They are false because you use ' == ' instead of ' .equals(..)'
' == ' will check if their addresses in the memory are equal. equals() checks their value.
Know that strings have an endsWith() method..
' == ' will check if their addresses in the memory are equal. equals() checks their value.
Know that strings have an endsWith() method..
public boolean endOther(String a, String b) {
if (a.length() < b.length() && b.toLowerCase().substring(b.length() - a.length(), b.length()).equals(a.toLowerCase().substring(0, a.length())))
return true;
else if (b.length() < a.length() && a.toLowerCase().substring(a.length() - b.length(), a.length()).equals(b.toLowerCase().substring(0, b.length())))
return true;
else if (a.length() == b.length() && a.toLowerCase().substring(0, a.length() - 1).equals(b.toLowerCase().substring(0, b.length() - 1)))
return true;
else
return false;
}
If any of you have a simpler solution, please let me know. Oh, and here's the link to the problem:CodingBat Java String-2 endOther
#4
Posted 16 March 2011 - 04:05 PM
What I would do:
You can even put the toLowerCase on the return-line, but I don't like very long lines a lot.
public boolean endOther(String a, String b) {
a = a.toLowerCase();
b = b.toLowerCase();
return b.endsWith(a) || a.endsWith(b);
}
You can even put the toLowerCase on the return-line, but I don't like very long lines a lot.
#5
Posted 23 March 2011 - 05:00 AM
I know it's cheatin'ยด, but in a lot of these exercises you could simply use the StringBuilder (have a look here). Internally it does the same as expected from you (string modification via "String"'s own methods).
//Edit: its actually not done by strings own methods, but with the help of an array of chars
//Edit: its actually not done by strings own methods, but with the help of an array of chars
Edited by PhillipKessels, 31 March 2011 - 08:02 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









