1

Long story short, the snippets below is about converting the texted month to numbered month (ex, Jan -> 1). There's no error but in the end I keep getting 0 as the result of m. Where's the problem?

//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;
}    
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Tom Apr 24 '17 at 15:53

7 Answers7

4

Instead of doing

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

Use,

if (monw.equals("Jan")) {
            x = 1;
        }
vikiiii
  • 9,246
  • 9
  • 49
  • 68
4

Use the .equals() method of String.

if (monw.equals("Jan"))

When you use == operator, it compares the memory locations of the two objects and returns false. In other words, it only returns true if the same object is on both sides of equation. So you should use the .equals() method instead, which returns true if two different objects have the same value.

EDIT:
I just checked, @LazyCubicleMonkey is right. The == operator checks if locations in memory are the same. I created a class, overrided the hashCode() method, created two objects and printed obj1==obj2. It prints false.

Hannele
  • 9,301
  • 6
  • 48
  • 68
shift66
  • 11,760
  • 13
  • 50
  • 83
  • It does not compare hashcodes. It compares locations in memory. (You were probably thinking of the default implementation of hashCode, System.identityHashCode(Object x)). – LazyCubicleMonkey Jan 18 '12 at 05:09
3

You're using == rather than equals() for String comparison.

tjdett
  • 1,703
  • 1
  • 18
  • 23
  • how can you make sure that monw was not created using new String("some Value")? as in that case it would fail – dpsdce Jan 18 '12 at 05:59
  • The problem in the question was that `equals()` should be used rather than `==`. While `==` can work in rare cases (where it really is the same String object), `equals()` will work all the time. Ideally it should be called as `monw.equals("Jan")`, so that subclasses of String work too. – tjdett Jan 24 '12 at 04:11
  • Good point... assuming your point is that `String` is a final class. You can however create classes implementing `Comparable`, which is the reason you'd use `monw.equals("Jan")` instead of `"Jan".equals(monw)`. (In that case you'd want to use the `monw` implementation of `equals()`). – tjdett Jan 25 '12 at 01:23
3

Your problem of string comparision is very well explained by others so I will not rewrite that. Other than that...

In your case, if your month names are contant, it will be better to use a Map<String,Integer> of month names and value or Enum having month names. It will omit your long if...else..if...else conditions.


Enum example:

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 example:

    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"));
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • Yeah but I'm creating this for a BlackBerry Application so enum and map doesn't work (tried just now) but thanks anyway :D – Anthony Pangestu Jan 18 '12 at 05:38
  • @AnthonyPangestu, I'm pretty sure `Map` is present, it's called HashTable. – st0le Jan 18 '12 at 06:29
  • Btw the code runs well in the simulator but it gives "Uncaught expression: Jan". Since "Jan" is a string in the code, I think the error is located there. I think I should try to use Map like u suggested but how to use it? – Anthony Pangestu Jan 18 '12 at 08:06
  • @AnthonyPangestu : I have no experience in BB, but in java I have shown the way to do it in my answer. – Harry Joy Jan 18 '12 at 08:41
2

In Java7, you can use the switch statement, instead of if-else. It supports String too.

user unknown
  • 35,537
  • 11
  • 75
  • 121
Boni
  • 590
  • 1
  • 3
  • 11
1
if ("Jan".equals (monw))
{  
     x = 1;
}
user unknown
  • 35,537
  • 11
  • 75
  • 121
subodh
  • 6,136
  • 12
  • 51
  • 73
0

Another lean approach would be, to store the Names in a List:

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

The first returns 1, the second -1. For you, you would add +1 to the 0-based index, and check for it not being 0 in the end or -1 in the beginning.

I'm not sure whether this works on Blackberry.

user unknown
  • 35,537
  • 11
  • 75
  • 121