-1

I have data stored in DoubleTimeSeries as:

DoubleTimeSeries width = DoubleTimeSeries.getDoubleTimeSeries(connection, userId, "Width");
DoubleTimeSeries height = DoubleTimeSeries.getDoubleTimeSeries(connection, userId, "Height"); 

and I am adding the above results into ArrayList as:

ArrayList<String> myArray = new ArrayList<>();
for (int i = 0; i < width.size(); i++) {
    myArray.add(width.get(i) + "," + height.get(i) );
}
System.out.println("My Array: \n" + myArray);

My issue: Some of the width or height value is empty (it is NaN) in my file and when i am adding them into my ArrayList, I am getting such as:

[NaN,NaN, 25,50, 55, 60, NaN,NaN, 45,56]  

Now: How I can prevent (or ignore) these NaN values from adding into my ArrayList. I want the ArrayList results as:

[25,50, 55, 60, 45,56]

Thanks

Bilgin
  • 499
  • 1
  • 10
  • 25
  • 1
    Does this answer your question? [How do you test to see if a double is equal to NaN?](https://stackoverflow.com/questions/1456566/how-do-you-test-to-see-if-a-double-is-equal-to-nan) – cutiko Apr 27 '20 at 03:44
  • @cutiko Thanks for your comment. I already tried this, I wrote the if statement inside my for loop but did not work. – Bilgin Apr 27 '20 at 03:48
  • just add a test for the width : if (width.get(i)==NaN){i++} and do not use `for` use `do .. while` – maryem neyli Apr 27 '20 at 03:51

2 Answers2

3

Compare the value with NaN, and only add if false.

for (int i = 0; i < width.size(); i++) {
    if (!Double.isNaN(width.get(i)) && !Double.isNaN(height.get(i))) {
        myArray.add(width.get(i) + "," + height.get(i) );
    }
}
1

just add a test for the width : if (width.get(i)==NaN){i++} and do not use for use do .. while

maryem neyli
  • 467
  • 3
  • 20