java - How to make variables usable by other methods -
i'm trying make program calculates number of days between day today , day of birth (well, i'm still learning idea not bright). program takes date written user keyboard , calculates difference later outputted. i've made getdifference method calculates that.
the issue is, need variable day, month , year (inputted user) later used getdifference method. know, variables inside methods local, eclipse told me convert them static variables - guess that's not best idea, because (at least think so) initiated default values , answer 0.
the question - how possible make value might not static , can used in method ? i've tried setters,getters, think easier way, couldn't find it. have started coding not long time ago, directions , tips appreciated, because honest don't know java developer, cannot give code check.
public class howmanydaystest { static string yearinstr; static string monthinstr; static string dayinstr; static string strlong; static long daysbetween; temporal birthday; public void getdifference() { localdate today = localdate.now(); try{ birthday = localdate.of(integer.valueof(dayinstr),integer.valueof(monthinstr), integer.valueof(yearinstr)); } catch(numberformatexception ex) { system.out.println("error"); } try{ daysbetween = chronounit.days.between(birthday, today); } catch(nullpointerexception e) { system.out.println("error"); } strlong = long.tostring(daysbetween); system.out.println(strlong); system.out.println(daysbetween); system.out.println(birthday); } public howmanydaystest() { jframe frame= new jframe("how many days?"); jlabel question= new jlabel("please enter date of birth"); jtextfield d = new jtextfield("dd"); jtextfield m = new jtextfield("mm"); jtextfield y = new jtextfield("yyyy"); jlabel ans= new jlabel(); frame.setvisible(true); frame.setsize(500, 500); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(question,borderlayout.north); frame.add(d,borderlayout.west); frame.add(m,borderlayout.center); frame.add(y,borderlayout.east); frame.add(ans,borderlayout.south); d.addfocuslistener(new focuslistener() { @override public void focusgained(focusevent arg0) { d.settext(""); } @override public void focuslost(focusevent e) { // todo auto-generated method stub } }); m.addfocuslistener(new focuslistener() { @override public void focusgained(focusevent arg0) { m.settext(""); } @override public void focuslost(focusevent e) { // todo auto-generated method stub } }); y.addfocuslistener(new focuslistener() { @override public void focusgained(focusevent arg0) { y.settext(""); } @override public void focuslost(focusevent e) { // todo auto-generated method stub } }); d.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { dayinstr = d.gettext(); integer.parseint(dayinstr); } }); m.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { monthinstr = m.gettext(); } }); y.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { yearinstr = y.gettext(); odp.settext(strlong); } }); } public static void main(string[] args) { howmanydaystest app = new howmanydaystest(); app.getdifference(); } }
declare day, month , year text fields instance variables , access them in getdifference()
method.
Comments
Post a Comment