Java中如何比较字符和数字

Java中如何比较字符和数字,java,string,numbers,int,compare,Java,String,Numbers,Int,Compare,我遇到了一个问题,我认为这是比较字符和数字的问题 String FindCountry = "BB"; Map<String, String> Cont = new HashMap <> (); Cont.put("BA-BE", "Angola"); Cont.put("9X-92", "Trinidad & Tobago"); for ( String key : Cont.keySet()) { if (key.charAt(0) == Fi

我遇到了一个问题,我认为这是比较字符和数字的问题

String FindCountry = "BB";

Map<String, String> Cont = new HashMap <> ();

Cont.put("BA-BE", "Angola");
Cont.put("9X-92", "Trinidad & Tobago");



for ( String key : Cont.keySet()) {

  if (key.charAt(0) == FindCountry.charAt(0) && FindCountry.charAt(1) >= key.charAt(1) && FindCountry.charAt(1) <= key.charAt(4)) {

    System.out.println("Country: "+ Cont.get(key));

  }
}
它不打印任何东西。我不确定我是否认为问题在于它不能比较“2”大于“Z”。在那个例子中,我只得到了两个Cont.put(),但在我的文件中,我得到了更多,而且其中很多不仅仅是字符。我对他们有意见

比较字符和数字的最聪明和最好的方法是什么?事实上,如果我设置一个规则,比如“1”大于“Z”,那就没问题了,因为我需要这个更大的方式:a-Z-9-0

谢谢

您可以使用查找“表”,我使用了
字符串

private static final String LOOKUP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
然后将chars与
indexOf()
进行比较,但是它看起来很混乱,而且可能更容易实现,我现在想不出更简单的方法:

String FindCountry = "9Z";

Map<String, String> Cont = new HashMap<>();

Cont.put("BA-BE", "Angola");
Cont.put("9X-92", "Trinidad & Tobago");

for (String key : Cont.keySet()) {
    if (LOOKUP.indexOf(key.charAt(0)) == LOOKUP.indexOf(FindCountry.charAt(0)) &&
        LOOKUP.indexOf(FindCountry.charAt(1)) >= LOOKUP.indexOf(key.charAt(1)) &&
        LOOKUP.indexOf(FindCountry.charAt(1)) <= LOOKUP.indexOf(key.charAt(4))) {
        System.out.println("Country: " + Cont.get(key));
    }
}
String FindCountry=“9Z”;
Map Cont=新的HashMap();
续(“BA-BE”、“安哥拉”);
续:“9X-92”,“特立尼达和多巴哥”);
for(字符串键:Cont.keySet()){
if(LOOKUP.indexOf(key.charAt(0))==LOOKUP.indexOf(FindCountry.charAt(0))&&
LOOKUP.indexOf(FindCountry.charAt(1))>=LOOKUP.indexOf(key.charAt(1))&&

LOOKUP.indexOf(FindCountry.charAt(1))如其他注释中所述,字符的此类数学比较操作基于每个字符的实际ASCII值。因此,我建议您使用作为参考重构您的逻辑。

如果您仅使用字符
A-Z
0-9
,您可以在两者之间添加一种转换方法,该方法将增加设置
0-9
字符的值,使其位于
A-Z
之后:

int applyCharOrder(char c){
  // If the character is a digit:
  if(c < 58){
    // Add 43 to put it after the 'Z' in terms of decimal unicode value:
    return c + 43;
  }
  // If it's an uppercase letter instead: simply return it as is
  return c;
}


PS:如中所述,变量通常以camelCase开头,因此它们不是以大写字母开头。

您可能希望实现正常的命名约定。如果所有内容看起来都像静态方法调用,那么读取代码非常困难。您在代码中使用的字符的ASCII值,如果您检查它,您将看到整数在ASCII表格中,rs的值小于字母字符。字符的数学运算(即
+
-
*
/
等)(包括
='2'
它将是
90>=50
,结果是
false
。所以你的问题确实是比较字符。@Lino在这种情况下,我会给你一个向上的投票,因为我使用了与第二种方法相同的方法。。)
int applyCharOrder(char c){
  // If the character is a digit:
  if(c < 58){
    // Add 43 to put it after the 'Z' in terms of decimal unicode value:
    return c + 43;
  }
  // If it's an uppercase letter instead: simply return it as is
  return c;
}
if(applyCharOrder(key.charAt(0)) == applyCharOrder(findCountry.charAt(0))
    && applyCharOrder(findCountry.charAt(1)) >= applyCharOrder(key.charAt(1))
    && applyCharOrder(findCountry.charAt(1)) <= applyCharOrder(key.charAt(4))){
  System.out.println("Country: "+ cont.get(key));
}
int applyCharOrder(char c){
  return "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".indexOf(c);
}