You are viewing our Forum Archives. To view or take place in current topics click here.
Program help (Java)
Posted:

Program help (Java)Posted:

Jackst09
  • Wise One
Status: Offline
Joined: May 24, 201014Year Member
Posts: 584
Reputation Power: 22
Status: Offline
Joined: May 24, 201014Year Member
Posts: 584
Reputation Power: 22
Hi im stuck on trying to do something. Basically i want my program to continue if the current month equals december (real time). if not an error message will appear and end the program. Any advice would be grateful cheers.
#2. Posted:
UnrealEgg
  • Powerhouse
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Something like this should do what you want. It's more pseudo code, you'll have to edit it to work for your project.


if(curMonth == "December" || curMonth == "12"){
   //Do something
} else {
   //Output error window
   System.exit
}
#3. Posted:
Jackst09
  • Wise One
Status: Offline
Joined: May 24, 201014Year Member
Posts: 584
Reputation Power: 22
Status: Offline
Joined: May 24, 201014Year Member
Posts: 584
Reputation Power: 22
UnrealEgg wrote Something like this should do what you want. It's more pseudo code, you'll have to edit it to work for your project.


if(curMonth == "December" || curMonth == "12"){
   //Do something
} else {
   //Output error window
   System.exit
}


Thanks i'll give it a try now
#4. Posted:
speed
  • 2 Million
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
UnrealEgg wrote Something like this should do what you want. It's more pseudo code, you'll have to edit it to work for your project.


if(curMonth == "December" || curMonth == "12"){
   //Do something
} else {
   //Output error window
   System.exit
}


Java requires .equals for string comparison. So it would be:
if(curMonth.equals("December"))
#5. Posted:
Jackst09
  • Wise One
Status: Offline
Joined: May 24, 201014Year Member
Posts: 584
Reputation Power: 22
Status: Offline
Joined: May 24, 201014Year Member
Posts: 584
Reputation Power: 22
I just used this and it seems to work; set to 10 at the moment as it wouldnt not work otherwise

import java.util.Calendar;

Calendar cal = Calendar.getInstance();

int month = cal.get(Calendar.MONTH);

if (month == 10) //meant to be 11 set to 10 to work
{
 System.out.print("Welcome to...");
       
}
else
{
    System.out.print("Not december!");
    System.exit(0);
}
#6. Posted:
UnrealEgg
  • Powerhouse
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49

if (month == 10) //meant to be 11 set to 10 to work


It's probably because the months are sorted in an array. As you should know, arrays start at 0 so it probably starts at 0 being January through to 11 being December. So it should be 11 in your if statement I believe?

Other than that, good to see you've got it working
#7. Posted:
Jackst09
  • Wise One
Status: Offline
Joined: May 24, 201014Year Member
Posts: 584
Reputation Power: 22
Status: Offline
Joined: May 24, 201014Year Member
Posts: 584
Reputation Power: 22
UnrealEgg wrote

if (month == 10) //meant to be 11 set to 10 to work


It's probably because the months are sorted in an array. As you should know, arrays start at 0 so it probably starts at 0 being January through to 11 being December. So it should be 11 in your if statement I believe?

Other than that, good to see you've got it working


Yeah cheers i did that for testing purposes that's all.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.