# Date form a csv file

**URL:** <https://discourse.processing.org/t/date-form-a-csv-file/380>\
**Category:** Coding Questions\
**Created:** [May 25, 2018, 7:49pm UTC](https://discourse.processing.org/t/date-form-a-csv-file/380 "2018-05-25T19:49:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dereklop](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@dereklop](https://discourse.processing.org/u/dereklop)\
**Post date:** [May 25, 2018, 7:49pm UTC](https://discourse.processing.org/t/date-form-a-csv-file/380/1 "2018-05-25T19:49:54Z")

</div>

hey im trying to get the date and separate values by it form a csv file i got. Im trying this but im not sure why its now working. here is a preview of my csv file [Imgur: The magic of the Internet](https://imgur.com/ND2GKw9)

> Cross-posted: [Reddit - Dive into anything](https://www.reddit.com/r/processing/comments/8m4dma/date_format_from_a_csv/)

```
size(2880,400);
background(0);

float w=0;
float h=0;
Table table = loadTable("indice_2018.CSV","header");
//Table row = new Table();

for(int i = 0; i<2880; i++){
    w = w + .6;
    h= h+ 14.4;
    String date = table.getString(i,"Fecha");
    String d = date.substring(0,2);
    String m = date.substring(3,5);
    String a = date.substring(6,10);
    
    //if ( d == "01"){
    
    
    int x= table.getInt(i,"Hora");
    int y = table.getInt(i,"Centro PM10");
    noStroke();
   
    if ( m == "01"){
    
    fill(100,255,255);
    }else{
    }fill(255,255,255);
    
    rect(w,y+200,2,2);

}

```

---

<div class="post-metadata">

**Author:** ![colouredmirrorball](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colouredmirrorball/32/234_2.png) [@colouredmirrorball](https://discourse.processing.org/u/colouredmirrorball)\
**Post date:** [May 25, 2018, 7:54pm UTC](https://discourse.processing.org/t/date-form-a-csv-file/380/2 "2018-05-25T19:54:25Z")

</div>

What exactly is not working? What do you get and what did you expect instead?

---

<div class="post-metadata">

**Author:** ![dereklop](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@dereklop](https://discourse.processing.org/u/dereklop)\
**Post date:** [May 25, 2018, 8:03pm UTC](https://discourse.processing.org/t/date-form-a-csv-file/380/3 "2018-05-25T20:03:16Z")

</div>

hi. Its not filling the dots with Blue. the if statment is not doing anything

---

<div class="post-metadata">

**Author:** ![colouredmirrorball](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colouredmirrorball/32/234_2.png) [@colouredmirrorball](https://discourse.processing.org/u/colouredmirrorball)\
**Post date:** [May 25, 2018, 8:07pm UTC](https://discourse.processing.org/t/date-form-a-csv-file/380/4 "2018-05-25T20:07:27Z")

</div>

Ah, indeed. You are doing String comparision the wrong way. This is a quirk of Java (and Processing) that is not obvious to a beginner at all. You can’t use `==` to compare two Strings. The correct way to compare two Strings is to use [`equals()`](https://processing.org/reference/String_equals_.html).

So the if statement becomes `if(m.equals("01"))`

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [May 25, 2018, 8:07pm UTC](https://discourse.processing.org/t/date-form-a-csv-file/380/5 "2018-05-25T20:07:46Z")

</div>

I’d start by [debugging your code](http://happycoding.io/tutorials/processing/debugging) to understand exactly what’s going on. For example what is the value of `m`?

I’d also try to isolate the problem: is the problem in reading the file, in comparing the value, something else? Try to come up with a smaller [MCVE](https://stackoverflow.com/help/mcve) that demonstrates what’s going on without any extra code.

But the first thing that jumps out at me is you’re comparing `String` values using the `==` operator, which is almost never what you want.

From [the Processing reference](https://processing.org/reference/String.html):

> To compare the contents of two Strings, use the `equals()` method, as in `if (a.equals(b))`, instead of `if (a == b)`. A String is an Object, so comparing them with the `==` operator only compares whether both Strings are stored in the same memory location. Using the `equals()` method will ensure that the actual contents are compared. (The [troubleshooting](http://wiki.processing.org/w/Troubleshooting#Why_don.27t_these_Strings_equal.3F) reference has a longer explanation.)

---

<div class="post-metadata">

**Author:** ![dereklop](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@dereklop](https://discourse.processing.org/u/dereklop)\
**Post date:** [May 25, 2018, 8:12pm UTC](https://discourse.processing.org/t/date-form-a-csv-file/380/6 "2018-05-25T20:12:46Z")

</div>

thanks its working yeiii!! yeii another question. Now my if statement is working and i have a string value of each month “m” and day “d”. Is this the best way to keep track of my dates?

---

<div class="post-metadata">

**Author:** ![colouredmirrorball](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colouredmirrorball/32/234_2.png) [@colouredmirrorball](https://discourse.processing.org/u/colouredmirrorball)\
**Post date:** [May 25, 2018, 8:19pm UTC](https://discourse.processing.org/t/date-form-a-csv-file/380/7 "2018-05-25T20:19:16Z")

</div>

That really depends on what you want to do. For some purposes it might be easier to [convert the String to an integer](https://processing.org/reference/intconvert_.html). Then you can eg. calculate a position based on the numerical value of the month.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [May 25, 2018, 8:19pm UTC](https://discourse.processing.org/t/date-form-a-csv-file/380/8 "2018-05-25T20:19:42Z")

</div>

It really depends what you’re going to do with them. Storing them as individual values is fine, or you could use the `Date` object from Java. You could also look into the new [date features](http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html) that were added in Java 8.

But again, it depends entirely on what you want to do with the values.

---

<div class="post-metadata">

**Author:** ![dereklop](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@dereklop](https://discourse.processing.org/u/dereklop)\
**Post date:** [May 25, 2018, 8:26pm UTC](https://discourse.processing.org/t/date-form-a-csv-file/380/10 "2018-05-25T20:26:52Z")

</div>

So this is the graph im making.

[https://imgur.com/ZPOYMjA](https://imgur.com/ZPOYMjA)

I have value acording to each hour of the day and colors represent each month. here is the code. Lets say i want to average the value of each month and graph how they are changing. Should i ask in my if statement to add all the value and then divide by the lenght of the rows?

```
size(2880,400);
background(0);

float w=0;
float h=0;
Table table = loadTable("indice_2018.CSV","header");
//Table row = new Table();

for(int i = 0; i<2880; i++){
    w = w + .6;
    h= h+ 14.4;
    String date = table.getString(i,"Fecha");
    String d = date.substring(0,2);
    String m = date.substring(3,5);
    String a = date.substring(6,10);
    
    //if ( d == "01"){
    
    
    int x= table.getInt(i,"Hora");
    int y = table.getInt(i,"Centro PM10");
    noStroke();
    rect(w,y+200,2,2);
   
   if(m.equals("01")){
    fill(#FAFF00);
   }else if(m.equals("02")) {
    fill(#00FF63);
    }else if(m.equals("03")) {
      fill(#008AFF);
    }else if(m.equals("04")) {
       fill(#FF002F);
    }else{
}

    }
```

---

<div class="post-metadata">

**Author:** ![dereklop](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@dereklop](https://discourse.processing.org/u/dereklop)\
**Post date:** [May 25, 2018, 8:27pm UTC](https://discourse.processing.org/t/date-form-a-csv-file/380/11 "2018-05-25T20:27:42Z")

</div>

Im trying to get te sum of each month to graph the average. instead of every hour.
