0

I am trying to get the returned value from a class, however I believe the string.format() is causing an error resulting in no value to return.

Class:

public class FilterTime {
    public String getData(String day, Integer time){
        // define the result
        String result = "";
        String convertedDay = "";

        if(day == "Friday 30th August"){
            convertedDay = "30";
        }
        if(day == "Saturday 31st August"){
            convertedDay = "31";
        }
        if(day == "Sunday 1st September"){
            convertedDay = "01";
        }

        if(time == null){
            result = "http://www.website.org/json.php?f=%s&type=date".format(convertedDay);
            Log.d("RESULT", "r:" + result);
        }else{
            result = "http://www.website.org/json.php?f=%s&time=@d&type=dateAndTime".format(convertedDay, time);
            Log.d("RESULT", "r:" + result);
        }

        return result;
    }
}

When I trace result in my Activity:

FilterTime filterTime = new FilterTime();
String filteredURL = filterTime.getData(dayFilter, timeFilter);

When I trace filteredURL it returns nothing at all. So I then put the Log.d() into the class and I found that when tracing the following it also returns nothing:

if(time == null){
                result = "http://www.website.org/json.php?f=%s&type=date".format(convertedDay);
                Log.d("RESULT", "r:" + result);
            }else{
                result = "http://www.website.org/json.php?f=%s&time=@d&type=dateAndTime".format(convertedDay, time);
                Log.d("RESULT", "r:" + result);
            }

I cannot understand where the error is coming from because there are no errors, just a warning saying it should be accessed in a static way, but I think the error resides in the if statement.

1
  • 1
    day == "Friday 30th August" <-- NO. this compares the references, NOT the contents. To compare the contents, use .equals(): day.equals("..."). Archi duplicate SO question, that one. Commented Jul 3, 2013 at 11:11

3 Answers 3

3

Use equals() to compare the contents of the String :

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Hence change your code to :

if("Friday 30th August".equals(day)){
        convertedDay = "30";
}

== operator compares object references, variable which contains a reference to an object. It checks if the references point to the same object.

P.S.:- Invoked equals() on the String literal to avoid any NPE due to null day.

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

4 Comments

Oh right! I didnt realise this was the problem!! What's the difference between a reference and the value?
Read this .
So it is kinda like a pointer?
Not exactly but yes you can assume that.
0

You have incorrect String comparison, instead of == use equals.

format is printing nothing since convertedDay remains empty "" due to invalid String comparison.

Comments

0

String.format() is a static method. Don't call it on a String object, just call it directly like this:

String.format("http://www.website.org/json.php?f=%s&type=date", convertedDay);

That should do the formatting like you wanted

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.