Reputation: 21
I'm a complete novice in Java. I'm writing a program which got stuck in an infinite loop which is displayed in the console but not in the Applet. It's a calendar which needs to change the month and date form October 31st to November 1st and display this in a location. I'm pretty sure the if statement is wrong but I can't find anything in my books to help :( here is the code:
int date = 28;
String currentMonth = "October";
String nextMonth = "November";
String dateNumber = "28th October";
for (date = 28; date <= 32; date++)
{
if (date == 32);
{
currentMonth = nextMonth;
date = 1;
}
switch (date)
{
case 28: dateNumber = "28th October"; break;
case 29: dateNumber = "29th October"; break;
case 30: dateNumber = "30th October"; break;
case 31: dateNumber = "31st October"; break;
case 32: dateNumber = "1st November"; break;
default: println (dateNumber); break;
}
GLabel label = new GLabel(dateNumber);
label.setFont ("Ariel-13");
label.setColor(Color.BLUE);
add (label, 50, 001 + (100*date));
}
In the console it repeats 28th October infinitely. In the Applet it shows "28th October" in the first position of the GLabel
only (it does not execute the + (100*date)
).
I would be very grateful if somebody can explain what's happening and suggest a way to fix it!
Upvotes: 0
Views: 62
Reputation: 21961
You put semicolon ;
at the end of if
statement.
if (date == 32) // Remove ;
{
Due to semicolon at the end of if
, won't enter on if
block, So your date
is not re-initiate to 1
.
Upvotes: 5