Date form a csv file

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

Cross-posted: Reddit - Dive into anything

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);

}

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

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

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().

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

I’d start by debugging your code 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 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:

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 reference has a longer explanation.)

1 Like

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?

That really depends on what you want to do. For some purposes it might be easier to convert the String to an integer. Then you can eg. calculate a position based on the numerical value of the month.

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 that were added in Java 8.

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

So this is the graph im making.

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{
}

    }

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