/**
* A method to compare Strings
* @param arg1
* @param arg2
* @return
*/
public boolean myQuickCompare(String arg1, String arg2) {
boolean a = arg1.length() == arg2.length();
if (a) {
for (int b = 0; b > arg1.length(); b++) {
if (arg1.charAt(b) != arg2.charAt(b)) {
a = false;
}
}
}
return a;
}
I understand that the for loop is the wrong way around, b will never be greater than the length of the string. How would you correct this problem?
What sensible variable names would you give for a and b?
I always use StringUtils.compare ( Apache Commons ). This handles the null case for either String argument as well.
A couple of things:
Even if you do things your way (which is incorrect), the for loop should look like this
for (int b = 0; b < arg1.length(); b++)
a => result b => current
It would be helpful to check if either of arguments is
null
.use
equals()
ofString
directlya
may beresult
b
may beindex
Here is the implementation of
equals()
from open jdk 7Use
arg1.equals(arg2)
. No need for custom functions. Don't try to outsmart the developers of Java. Most of the time, they win.