java - Method returning wrong value -
i'm creating program receives current date , s tomorrow's date. working fine when tried entering day 30 4th month, instead of taking last day of month , going next month, added 1 more day , returned 31. later found out method sets maximum day each month returning maximum day 0. when try putting code method
actionperformed
it works fine in method made keeps returning maximum day zero, i've tried using different variables , other things nothing's working.
here's code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class tomorrow extends jframe implements actionlistener { jlabel dayl; jlabel monthl; jlabel yearl; jtextfield dayt; jtextfield montht; jtextfield yeart; jbutton enter; public static void main(string[] args) { tomorrow frame=new tomorrow(); frame.setsize(400, 400); frame.setlocation(500, 300); frame.creategui(); frame.setvisible(true); frame.settitle("enter current date"); } void creategui(){ setdefaultcloseoperation(exit_on_close); container window=getcontentpane(); window.setlayout(new flowlayout(5)); dayl=new jlabel("enter day today's date"); window.add(dayl); dayt=new jtextfield(10); window.add(dayt); monthl=new jlabel("enter month today's date"); window.add(monthl); montht=new jtextfield(10); window.add(montht); yearl=new jlabel("enter year today's date"); window.add(yearl); yeart=new jtextfield(10); window.add(yeart); enter=new jbutton("enter"); window.add(enter); enter.addactionlistener(this); } int maxiday(int month, int day, int year){ if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)){ day=31; }else if((month==4)||(month==6)||(month==9)||(month==11)){ day=30; }else if(month==2){ if((year%400==0)||(year%100!=0&&year%4==0)){ if(day>28){ joptionpane.showmessagedialog(null, "highest day month 28"); day=28; } }else{ if(day>29){ joptionpane.showmessagedialog(null, "highest day month 29"); day=29; } } } return day; } int todday; int todmonth; int todyear; int tomday; int tommonth; int tomyear; int maxday; public void actionperformed(actionevent e) { if ((!dayt.gettext().isempty())||(!montht.gettext().isempty())||(!yeart.gettext().isempty())){ todday=integer.parseint(dayt.gettext()); todmonth=integer.parseint(montht.gettext()); todyear=integer.parseint(yeart.gettext()); } maxiday(todmonth, maxday, todyear); if(todday>maxday){ if(maxday==31){ joptionpane.showmessagedialog(null, "the highet day month 31"); todday=31; }else if(maxday==30){ joptionpane.showmessagedialog(null, "the highet day month 30"); todday=30; } } if(todday==maxday){ tomday=1; tommonth=todmonth+1; }else if((todmonth==12)&&(todday==31)){ tomday=1; tommonth=1; tomyear=todyear+1; } tomday=todday+1; tommonth=todmonth; tomyear=todyear; joptionpane.showmessagedialog(null, "(dd/mm/yyyy)" + "\ntomorrow's date is: " + tomday + "/" + tommonth + "/" + tomyear + maxday); } }
i need know i'm doing wrong p.s can't rid of method made , put in actionperformed, it's assignment , has done way.
1.you haven't stored return value function
maxiday(todmonth, maxday, todyear);
2.you using variable named 'maxday' compare not initialised.
if(todday>maxday){ if(maxday==31){ joptionpane.showmessagedialog(null, "the highet day month 31"); todday=31; }else if(maxday==30){
it should
maxday = maxiday(todmonth, maxday, todyear); if(todday>maxday){ if(maxday == 31)....
Comments
Post a Comment