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

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It does not compare hashcodes. It compares locations in memory. (You were probably thinking of the default implementation of hashCode, System.identityHashCode(Object x)).
4

Instead of doing

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

Use,

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

Comments

3

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

3 Comments

how can you make sure that monw was not created using new String("some Value")? as in that case it would fail
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.
Good point... assuming your point is that String is a final class. You can however create classes implementing Comparable<String>, 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()).
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"));

4 Comments

Yeah but I'm creating this for a BlackBerry Application so enum and map doesn't work (tried just now) but thanks anyway :D
@AnthonyPangestu, I'm pretty sure Map is present, it's called HashTable.
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?
@AnthonyPangestu : I have no experience in BB, but in java I have shown the way to do it in my answer.
2

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

1 Comment

@AnthonyPangestu: You should mention BlackBerry in your post, then, and use the BlackBerry flag.
1
if ("Jan".equals (monw))
{  
     x = 1;
}

1 Comment

It's generally helpful to include the reason why the corrected code works.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.