We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Beginner: Changing month() to a name
Page Index Toggle Pages: 1
Beginner: Changing month() to a name (Read 433 times)
Beginner: Changing month() to a name
Jan 25th, 2008, 4:48pm
 
I'm having trouble changing the month() from a number to a word.

I'm new to C programming, so I'm guessing I'm missing something simple. Maybe it's because I can't change a variable to a word?

I thought this would change it:

if(month() == 1){
  month() = "JAN";
   }

Thanks for your help.



---------------------My Code--------------------------
PFont font;

void setup() {
 size(screen.width,screen.height);
 //frame.setResizable(true);
}

void draw() {
 
   background(255);
   //rect(10, 10, width-20, height-20);
 
 font = loadFont("Univers45.vlw");
 textFont(font, 30);
 
 
 background(0);
 int s = second();
 int m = minute();
 int h = hour();
 int d = day();
 int o = month();
 int y = year();
 
 //time
 String t = nf(h,2) + ":" + nf(m,2) + ":" + nf(s,2);
 text(t, screen.width-160, screen.height-60);
 //day
 String a = nf(d,2) + " " + nf(o,2) + " " + nf(y,2);
 text(a, screen.width-160, screen.height-30);
}
Re: Beginner: Changing month() to a name
Reply #1 - Jan 25th, 2008, 4:57pm
 
You can't assign a value to a function, which is what you're trying to do.
What you need to do is have a String variable, and you assign the right name to that variable, depending on the value that month() returns to you.

e.g.
Code:

String m;
if(month() == 1)
m="Jan";
else if(month() == 2)
m="Feb";
//etc etc
Re: Beginner: Changing month() to a name
Reply #2 - Jan 25th, 2008, 5:38pm
 
Another way is to save your month names in an array.
Code:

String[] monthNames=["jan", "feb", ...]
String month = monthNames[month()-1];
Re: Beginner: Changing month() to a name
Reply #3 - Jan 25th, 2008, 8:37pm
 
Now I get this error:

Semantic Error: No applicable overload for a method with signature "nf(java.lang.String, int)" was found in type "processing.core.PApplet". Perhaps you wanted the overloaded version "java.lang.String[] nf(int[] $1, int $2);" instead?

I know this error is telling me that nf will not work with the new variable. I don't know how to properly code this.

Any help would be appreciated.

Thanks.



---------------------My latest code-------------------
PFont font;

void setup() {
 size(screen.width,screen.height);
 //frame.setResizable(true);
}

void draw() {
 
   background(255);
   //rect(10, 10, width-20, height-20);
 
 font = loadFont("Univers45.vlw");
 textFont(font, 30);
 
 
 background(0);
 int s = second();
 int m = minute();
 int h = hour();
 int d = day();
 //int o = month();
 int y = year();
 
  String o;
if(month() == 1)
 o="Jan";
else if(month() == 2)
 o="Feb";
else if(month() == 3)
 o="Mar";
else if(month() == 4)
 o="Apr";
else if(month() == 5)
 o="May";
else if(month() == 6)
 o="Jun";
else if(month() == 7)
 o="Jul";
 
 
 
 //time
 String t = nf(h,2) + ":" + nf(m,2) + ":" + nf(s,2);
 text(t, screen.width-160, screen.height-60);
 //day
 String a = nf(d,2) + " " + nf(o,2) + " " + nf(y,2);
 text(a, screen.width-160, screen.height-30);
}
Re: Beginner: Changing month() to a name
Reply #4 - Jan 25th, 2008, 8:40pm
 
Your month string "o" is a string not an int, so you don't need to pass it through nf. so
Code:
" " + nf(o,2) + " " 


Should be
Code:
" " + o + " " 

Re: Beginner: Changing month() to a name
Reply #5 - Jan 25th, 2008, 9:01pm
 
I changed it to:

 //day
 String a = nf(d,2) + " " + o + " " + nf(y,2);
 text(a, screen.width-160, screen.height-30);

And I get the error:
Semantic Error: The variable "o" may be accessed here before having been definitely assigned a value.

Is the "String o" section not working?
Re: Beginner: Changing month() to a name
Reply #6 - Jan 25th, 2008, 9:03pm
 
Change 'String o;' to 'String o = "";'
Re: Beginner: Changing month() to a name
Reply #7 - Jan 25th, 2008, 9:08pm
 
Wow, Thanks!
Page Index Toggle Pages: 1