0

I'm trying to get array of int values from JSON, however something strange is happening and i get different output when i read the same array on two places in the code.

Does anyone know the reason behind this behaviour?

public LineGraphSeries<DataPoint> fillGraph(String city, int numberOfDays){
    int data[] = new int[numberOfDays*8];
    DataPoint[] temperatures = new DataPoint[numberOfDays*8];
    LineGraphSeries<DataPoint> graph;
    String url = "http://api.worldweatheronline.com/free/v2/weather.ashx?q=" + city + "&format=json&num_of_days=" + numberOfDays + "&cc=no&fx24=no&show_comments=no&tp=3&key=e74975c820b1f6506bd6b9fdea5a5";
    JSONObject dataZNetu;
    JSONArray dataArray;
    JSONObject dataHourly;
    JSONArray dataHourlyArray;
    try {
        dataZNetu = requestWebService(url).getJSONObject("data");
        dataArray = dataZNetu.getJSONArray("weather");
        for(int i = 0; i<dataArray.length(); i++){
            dataHourly = dataArray.getJSONObject(i);
            dataHourlyArray = dataHourly.getJSONArray("hourly");
            for(int j = 0; j<dataHourlyArray.length(); j++){
                data[i*j] = dataHourlyArray.getJSONObject(j).getInt("FeelsLikeC");
                //temperatures[i*j] = new DataPoint(i*j,data[i*j]);
                Log.v("dataCorrect" + i,String.valueOf(data[i*j])); //Correct values
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    for(int i = 0;i<data.length;i++){
        Log.v("dataBroken" + i/8,String.valueOf(data[i])); //Broken values
    }

    graph = new LineGraphSeries<DataPoint>(temperatures);
    return graph;
}

Log:

05-15 02:05:50.609  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 6

05-15 02:05:50.609  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 5

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 12

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 18

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 19

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 20

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 16

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 14

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 10

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 7

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 16

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 20

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 24

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 24

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 18

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 15

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 11

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 11

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 15

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 16

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 17

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 17

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 14

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 11

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 11

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 7

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 11

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 20

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 15

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 24

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 16

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 15

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 17

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 17

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 14

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 11

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0
3
  • 1
    data[i*j]??? Are you sure? Maybe you mean something like data[(i*8)+j]? Whatever - your indexing arithmetic is completely different between the "Correct: and "Broken" examples :( Commented May 15, 2015 at 0:19
  • 1
    I think he meant i*dataHourlyArray.length()+j. Of course even that may be wrong if dataHourlyArray.length() isn't a constant for all indexes in dataArray Commented May 15, 2015 at 0:21
  • thanx, it was bad indexing as FoggyDay described... never work too long into night guys. Commented May 15, 2015 at 9:11

1 Answer 1

1

You need to change your array declaration as well as the way you are accessing it. Instead of using * or multiplication during initialization, just a multi-dimension array and use [][] instead of performing arithmetic operation on the array.

 int [][]data = new int[numberOfDays][8];
 String url = "http://api.worldweatheronline.com/free/v2/weather.ashx?q=" + city + "&format=json&num_of_days=" + numberOfDays + "&cc=no&fx24=no&show_comments=no&tp=3&key=e74975c820b1f6506bd6b9fdea5a5";
 JSONObject dataZNetu;
 JSONArray dataArray;
 JSONObject dataHourly;
 JSONArray dataHourlyArray;
    try
    {
      WebContext webContext = new WebContext();
      dataZNetu = webContext.DownloadJSON(url).getJSONObject("data");
      dataArray = dataZNetu.getJSONArray("weather");
      for(int i = 0; i<dataArray.length(); i++)
      {
        dataHourly = dataArray.getJSONObject(i);
        dataHourlyArray = dataHourly.getJSONArray("hourly");
        for(int j = 0; j<dataHourlyArray.length(); j++){
          data[i][j] = dataHourlyArray.getJSONObject(j).getInt("FeelsLikeC");
          //temperatures[i*j] = new DataPoint(i*j,data[i*j]);
          Log.v("dataCorrect" + i,String.valueOf(data[i][j])); //Correct values
        }
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    for(int i = 0;i<data.length;i++)
    {
      Log.v("dataBroken" + i/8,String.valueOf(data[i])); //Broken values
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Is it a general rule of thumb to add a dimension instead of performing arithmetic? Seems safer, but i have never been recommended to use this approach. Anyway, your solution would allso solve this situation as it will make the code easier to grasp. Thanx

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.