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!

regular expressions

Status
Not open for further replies.

kanghao

IS-IT--Management
Jul 4, 2004
68
KR
When I call the method below, "java.lang.IllegalStateException: No match found" I get.
Why?

Thanks.

int res = StringUtil.compareString("1a","2a");

package debug;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class StringUtil {
public static int compareString(String first, String second) {
String reg_pattern = "[a-z]";
Pattern pattern = Pattern.compile(reg_pattern);
Matcher matcher_first = pattern.matcher(first);
Matcher matcher_second = pattern.matcher(second);
boolean matched_first = matcher_first.find();
boolean matched_second = matcher_second.find();
if(matched_first && matched_second) {
String num_in_first = matcher_first.replaceAll("");
String letter_in_first = matcher_first.group();
String num_in_second = matcher_second.replaceAll("");
String letter_in_second = matcher_second.group();
if(num_in_first.equals(num_in_second)){
return letter_in_first.compareTo(letter_in_second);
}else{
return compareNumberAsString(num_in_first,num_in_second);
}
} else if(!matched_first && matched_second) {
String num_in_first = first;
String num_in_second = matcher_second.replaceAll("");
return compareNumberAsString(num_in_first,num_in_second);
} else if(matched_first && !matched_second) {
String num_in_first = matcher_first.replaceAll("");
String num_in_second = second;
return compareNumberAsString(num_in_first,num_in_second);
} else {//
return compareNumberAsString(first,second);
}
}
public static int compareNumberAndAlphaNumber(String param_1, String param_2){
return 1;
}
public static int compareNumberAsString(String first,String second){
int length_longer;
String first_str = Integer.parseInt(first) + "";
String second_str = Integer.parseInt(second) + "";

if(first_str.length()>second_str.length()){
length_longer = first_str.length();
for(int i = 1; i < length_longer; i++) {
second_str = "0" + second_str;
}
}else if(first_str.length()<second_str.length()){
length_longer = second_str.length();
for(int i = 1; i < length_longer; i++) {
first_str = "0" + first_str;
}
}
return first_str.compareTo(second_str);
}
public static boolean isPatternMatched(String reg_pattern, String target_str) {
Pattern pattern = Pattern.compile(reg_pattern);
Matcher matcher = pattern.matcher(target_str);
return matcher.find();
}

}
 
it's supposed to be
String letter_in_first = matcher_first.group();
String num_in_first = matcher_first.replaceAll("");
not
String num_in_first = matcher_first.replaceAll("");
String letter_in_first = matcher_first.group();

How dumb of me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top