Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing hashcode value of 2 strings

Status
Not open for further replies.

ob1kanobee

Programmer
Dec 27, 2002
47
US
Is there a better or more efficient way of assessing this information? Values being compared will always be the same length and are already being verified for Letter or Digit values only. Values may be all numeric or all alphabetic or combination of both. I want to know when first value is greater than second value.

String value1 = "A22B7A9X";
String value2 = "A22C7A9X";

boolean isGreater = false;
for(int x = 0 ;x < value1.length(); x++)
{
if(value1.substring(x,x+1).hashCode() > value2.substring(x,x+1).hashCode())
{
isGreater = true;
break;
}
}
if(isGreater)
System.out.println("Value 1 IS greater than Value 2");
else
System.out.println("Value 1 IS NOT greater than Value 2");
 
1.- When one value is supposed to be greater than another? Alphabetically?

2.- What makes you think the hashcode is a way to sort Strings?

Cheers,
Dian
 
Who said anything about sorting a string. I just want to know if first value is greater than second value. I forgot to include this, but both strings will be converted to upper case prior to verification.
What I need this for is to be able to evaluate Zip Codes, Canadian Postal Codes, Mexican Postal Codes, etc.
Before it gets to this point, I will already know what the Postal Code country is so the comparison will be for 2 postal codes within the same country.
 
Apparently not good enough.
Try this:
String value1 = "A22C7A9W";
String value2 = "K22C7A9W";

System.out.println("Value = " + value1.compareTo(value2));

Assuming that .compareTo returns -1 for less than, 0 for equal and 1 for greater than, the above is returning a -10 when in fact value2 IS greater than value1.
 
I take that back, I think this will work.
Thanks feherke!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top