【问题标题】:String to int conversion字符串到 int 的转换
【发布时间】:2012-01-18 05:03:19
【问题描述】:

长话短说,下面的 sn-ps 是关于将文本月份转换为编号月份(例如 Jan -> 1)。没有错误,但最后我一直得到 0 作为 m 的结果。问题出在哪里?

//date1s[] is the result of splitting date1 by spaces (date1 = 1 Jan 2012)
m = convertMonth(date1s[1]); //date1s[1] contains the Month; date1s[0] the date and date1s[2] the year

public int convertMonth(String monw) {
        int x = 0;
        if (monw == "Jan") {
            x = 1;
        }
        else if (monw == "Feb") {
            x = 2;
        } 
        else if (monw == "Mar") {
            x = 3;
        } 
        else if (monw == "Apr") {
            x = 4;
        } 
        else if (monw == "May") {
            x = 5;
        } 
        else if (monw == "Jun") {
            x = 6;
        } 
        else if (monw == "Jul") {
            x = 7;
        } 
        else if (monw == "Aug") {
            x = 8;
        } 
        else if (monw == "Sep") {
            x = 9;
        } 
        else if (monw == "Oct") {
            x = 10;
        } 
        else if (monw == "Nov") {
            x = 11;
        } 
        else if (monw == "Dec") {
            x = 12;
        }
        return x;
}    

【问题讨论】:

标签: java arrays string int


【解决方案1】:

使用String的.equals()方法。

if (monw.equals("Jan"))

当您使用== 运算符时,它会比较两个对象的内存位置并返回false。换句话说,它只返回true,如果同一个对象在等式的两边。所以你应该改用.equals() 方法,如果两个不同的对象具有相同的值,它会返回true

编辑:
我刚刚检查过,@LazyCubicleMonkey 是对的。 == 运算符检查内存中的位置是否相同。我创建了一个类,覆盖了hashCode() 方法,创建了两个对象并打印了obj1==obj2。它打印false

【讨论】:

  • 它不比较哈希码。它比较内存中的位置。 (你可能想到了 hashCode 的默认实现 System.identityHashCode(Object x))。
【解决方案2】:

而不是做

if (monw == "Jan") {
            x = 1;
        }

使用,

if (monw.equals("Jan")) {
            x = 1;
        }

【讨论】:

    【解决方案3】:

    您使用== 而不是equals() 进行字符串比较。

    【讨论】:

    • 如何确定 monw 不是使用 new String("some Value") 创建的?在那种情况下它会失败
    • 问题中的问题是应该使用equals()而不是==。虽然== 可以在极少数情况下工作(它实际上是同一个字符串对象),但equals() 将一直工作。理想情况下它应该被称为monw.equals("Jan"),这样String的子类也可以工作。
    • 好点...假设您的观点是String 是最后一课。但是,您可以创建实现Comparable<String> 的类,这就是您使用monw.equals("Jan") 而不是"Jan".equals(monw) 的原因。 (在这种情况下,您需要使用 equals()monw 实现)。
    【解决方案4】:

    其他人很好地解释了您的字符串比较问题,所以我不会重写。除此之外……

    在您的情况下,如果您的月份名称是连续的,则 更好 使用月份名称和值的 Map<String,Integer> 或具有月份名称的 Enum .它将省略您的长 if...else..if...else 条件。


    枚举示例:

    public enum Month {
        Jan,
        Feb,
        Mar,
        Apr,
        // ...
        Dec
    }    
    public int toMonthInt(String input) {
        return Month.valueOf(input).ordinal() + 1; // +1 because you want Month value to start with 1.
    }
    

    地图示例:

        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("Jan", 1);
        map.put("Feb", 2);
        map.put("Mar", 3);
        map.put("Apr", 4);
        // ...
        map.put("Dec", 12);
        System.out.println(map.get("Jan"));
    

    【讨论】:

    • 是的,但我正在为 BlackBerry 应用程序创建这个,所以枚举和地图不起作用(刚刚尝试过)但无论如何谢谢:D
    • @AnthonyPangestu,我很确定 Map 存在,它被称为 HashTable。
    • 顺便说一句,代码在模拟器中运行良好,但它给出了“未捕获的表达式:Jan”。由于“Jan”是代码中的一个字符串,我认为错误就在那里。我想我应该像你建议的那样尝试使用 Map 但如何使用它?
    • @AnthonyPangestu :我在 BB 方面没有经验,但在 java 中,我已经在我的回答中展示了这样做的方法。
    【解决方案5】:

    在 Java7 中,您可以使用 switch 语句,而不是 if-else。它也支持字符串。

    【讨论】:

    • @AnthonyPangestu:那么,您应该在帖子中提及 BlackBerry,并使用 BlackBerry 标志。
    【解决方案6】:
    if ("Jan".equals (monw))
    {  
         x = 1;
    }
    

    【讨论】:

    • 包含更正代码有效的原因通常会有所帮助。
    【解决方案7】:

    另一种精益方法是将名称存储在列表中:

    List <String> as = Arrays.asList ("Jan", "Feb", "Mar");
    System.out.println (as.indexOf ("Feb")); 
    System.out.println (as.indexOf ("Fex")); 
    

    第一个返回 1,第二个返回 -1。对您而言,您可以将 +1 添加到基于 0 的索引中,并检查它是否最终不是 0 或开头不是 -1。

    我不确定这是否适用于黑莓。

    【讨论】:

      猜你喜欢
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多