Function doesn't return a string

FluFlu
edited June 2014 in Programming Questions

Hi! I am trying to get a function return a string so I can check some things. Anyway, it's better to show the code:

import com.onformative.yahooweather.*;
YahooWeather weather;
int updateIntervallMillis = 30000;
weather = new YahooWeather(this, 56594508, "c", updateIntervallMillis);

void setup() {
  size(200,200);
  frameRate(1);
}
String weatherCondition;
void draw() {
  weather.update();
  println(weatherCondition);
}
String weather.getWeatherCondition() {
  return weatherCondition;
}

So, after the function getWeatherCondition() returns the string, I need to check if it says that is saying "Light Rain", or "Cloudy" or any other things to trigger some events after this. It says that "expecting EOF,'weather' found" but when I put the "weather = new..." line in setup it still doesn't work. It says that I get null.

Answers

  • line 4 should be in setup()

    based on https://github.com/onformative/YahooWeather/blob/master/src/com/onformative/yahooweather/processing15/YahooWeather.java

    your function should be more like

    String getWeatherCondition() {
      return weather.getWeatherCondition;
    }
    

    where weather is your YahooWeather class, getWeatherCondition() is an accessor within it allowing you to read the member. but, you'll notice, that that method is pretty ridiculous as it stands.

  • Another way to do it then?

  • edited June 2014

    I always prefer the order

    • imports

    • consts

    • global vars

    • setup()

    • draw()

    • Input funcs

    • other funcs

    • classes

    you mixed funcs and global vars.

  • Answer ✓

    here

    import com.onformative.yahooweather.*;
    
    YahooWeather weather;
    int updateIntervallMillis = 30000;
    
    String weatherCondition;
    
    void setup() {
      size(200, 200);
      //  frameRate(1);
    
      weather = new YahooWeather(this, 56594508, "c", updateIntervallMillis);
    }
    
    void draw() {
      weather.update();
      println(getWeatherCondition());
    }
    
    //String weather.getWeatherCondition() {
    //  return weatherCondition;
    //}
    
    String getWeatherCondition() {
      return weather.getWeatherCondition();
    }
    
  • but that's what i said! 8)

    a method containing a single line, just returning a value from another method, usually isn't worth the overhead of calling it. so line 17 (of chrisir's code) could just be

    println(weather.getWeatherCondition());
    

    and you could delete lines 24-26.

    i'd've left the frameRate in there - no need to update 60 times a second when the data's only changing every 30 seconds (and maybe not even then - how often does weather change?)

  • yeah, absolutely what you've said, I just put it together

    ;-)

    in your code, koogs, in line 2 the ( ) was missing

    in his code line 13 was a mess

    so I wanted to try it...

    ;-)

  • ha, thanks, didn't spot those

  • edited June 2014

    ... usually isn't worth the overhead of calling it.

    As some legends say, Java VM can later inline such simple get()/set() calls! :-$
    Although if it's just 1 or 2 places that happen to call it, I don't bother! :-))

    Anyways, I've made a remix too. Check it out: =P~
    P.S.: UNTESTED! :@)

    // forum.processing.org/two/discussion/6060/
    // function-doesnt-return-a-string
    
    import com.onformative.yahooweather.*;
    
    static final int INTERVAL = 30000;
    
    final YahooWeather weather = new YahooWeather(
    this, 56594508, "c", INTERVAL);
    
    String weatherInfo;
    
    void setup() {
      size(100, 100, JAVA2D);
      noLoop();
      frameRate(1);
      clear();
    
      thread("retrieveWeather");
    }
    
    void draw() {
      println(weatherInfo);
    }
    
    void mouseClicked() {
      weather.update();
      getWeather();
    }
    
    void retrieveWeather() {
      for (;getWeather(); delay(INTERVAL));
    }
    
    boolean getWeather() {
      weatherInfo = weather.getWeatherCondition();
      redraw();
      return true;
    }
    
  • import com.onformative.yahooweather.*;
    
    YahooWeather weather;
    int updateIntervallMillis = 30000;
    
    String weatherCondition;
    
    void setup() {
      size(200, 200);
      //  frameRate(1);
    
      weather = new YahooWeather(this, 56594508, "c", updateIntervallMillis);
    }
    void draw() {
      weather.update();
      //println(getWeatherCondition());
    
      if (getWeatherCondition() == "Sunny") {
        println(1);
      }
    }
    
    //String weather.getWeatherCondition() {
    //  return weatherCondition;
    //}
    
    String getWeatherCondition() {
      return weather.getWeatherCondition();
    }
    

    Why doesn't this work then? It doesn't throw an error. It just doesn't write anything... What di I do wrong? This is Chrisir's example.

  • Answer ✓

    yeah, == doesn't work with String

    you need to say

    if (getWeatherCondition().equals("Sunny")) {

    (not tested)

    It's a pain...

    ;-)

Sign In or Register to comment.