简体   繁体   English

如何在Java中将char与数字进行比较

[英]How to compare char with number in Java

I got a problem and I think it is in comparing a char with a number. 我遇到了一个问题,我认为这是将char与数字进行比较。

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));

  }
}

In this case the code print "Angola", but if 在这种情况下,代码显示为“安哥拉”,但如果

String FindCountry = "9Z" 

it doesn't print anything. 它不会打印任何内容。 I am not sure I think the problem is in that it can't compare that is '2' greater than 'Z'. 我不确定问题是否在于无法比较“ Z”比“ 2”大。 In that example, I got only two Cont.put(), but in my file, I got much more and a lot of them are not only with chars. 在该示例中,我只有两个Cont.put(),但是在我的文件中,我得到了更多,而且很多不仅包含char。 I got a problem with them. 我对他们有问题。

What is the smartest and best way to compare char with a number ? 将char与数字进行比较的最聪明,最好的方法是什么? Actually, if I set a rule like "1" is greater than "Z" it will be okay because I need this way of greater: AZ-9-0. 实际上,如果我将“ 1”设置为大于“ Z”,则可以,因为我需要这种更大的方式:AZ-9-0。

Thanks! 谢谢!

You can use a lookup "table", I used a String : 您可以使用查找“表”,我使用String

private static final String LOOKUP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

And then compare the chars with indexOf() , but it seems messy and could probably be achieved more easily, I just can't come up with something easier at the moment: 然后将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));
    }
}

If you only use the characters AZ and 0-9 , you could add a conversion method in between which will increase the values of the 0-9 characters so they'll be after AZ : 如果仅使用字符AZ0-9 ,则可以在两者之间添加转换方法,这将增加0-9字符的值,因此它们将在AZ之后:

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;
}

Which can be used like this: 可以这样使用:

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));
}

Try it online. 在线尝试。

Note: Here is a table with the decimal unicode values. 注意: 这是带有十进制unicode值的表。 Characters '0'-'9' will have the values 48-57 and 'A'-'Z' will have the values 65-90 . 字符'0'-'9'将具有值48-57而字符'0'-'9' 'A'-'Z'将具有值65-90 So the < 58 is used to check if it's a digit-character, and the + 43 will increase the 48-57 to 91-100 , putting their values above the 'A'-'Z' so your <= and >= checks will work as you'd want them to. 所以< 58用于检查它是否是数字字符,而+ 43会将48-57增加到91-100 ,将其值放在'A'-'Z'上方,因此您的<=>=检查将按您希望的方式工作。


Alternatively, you could create a look-up String and use its index for the order: 另外,您可以创建一个查找字符串,并使用其索引作为顺序:

int applyCharOrder(char c){
  return "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".indexOf(c);
}

Try it online. 在线尝试。

PS: As mentioned in the first comment by @Stultuske , variables are usually in camelCase, so they aren't starting with an uppercase letter. PS:正如@Stultuske在第一条评论中所述 ,变量通常在camelCase中,因此它们不是以大写字母开头。

As the others stated in the comments, such mathematical comparison operations on characters are based on the actual ASCII values of each char. 正如其他评论中所述,对字符的这种数学比较操作基于每个字符的实际ASCII值。 So I'd suggest you refactor your logic using the ASCII table as reference. 因此,我建议您使用ASCII表作为参考来重构逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM