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.

Josh Boothe
  • 1,413
  • 4
  • 25
  • 40
  • 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. – fge Jul 03 '13 at 11:11

3 Answers3

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.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

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

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

harsh
  • 7,502
  • 3
  • 31
  • 32
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

David Roussel
  • 5,788
  • 1
  • 30
  • 35