java - JTextArea Displays 0 after calculate button clicked -


i'm writing code university, read user input , calculate charges (its hospital bill)

but when press calculate jtextarea displays 0 value

i'm newbie guidance appreciated

the code is:

import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class hospitalchargescalculator extends jframe implements actionlistener{  private jlabel hospitalstaylabel; private jlabel medicationlabel; private jlabel surgicalfeeslabel; private jlabel labfeeslabel; private jlabel rehablabel; private jlabel totallabel;  private jtextfield hospitalstaytf; private jtextfield medicationtf; private jtextfield surgicalfeestf; private jtextfield labfeestf; private jtextfield rehabtf;  private jtextarea totalchargesta;  private jbutton calculateb; private jbutton exitb;  public static final int width = 500; public static final int height = 350;  static int totalstaycharge; static int totalmisc; static int totalcharges;  static int totaldays; static int totalmedication; static int totalsurgical; static int totallab; static int totalrehab;  public hospitalchargescalculator() {     settitle("hospital charges");     container c = getcontentpane();     setsize(width, height);      c.setlayout(null);      hospitalstaylabel = new jlabel(" number of days spent in hospital: ",             swingconstants.left);     medicationlabel = new jlabel(" total medication charges: ",             swingconstants.left);     surgicalfeeslabel = new jlabel(" total sugical charges : ",             swingconstants.left);     labfeeslabel = new jlabel(" total lab fees: ",             swingconstants.left);     rehablabel = new jlabel(" total rehab charges: ",             swingconstants.left);     totallabel = new jlabel(" total charges: ",             swingconstants.left);      calculateb = new jbutton("calculate");     calculateb.addactionlistener(this);     exitb = new jbutton("exit");     exitb.addactionlistener(this);       hospitalstaytf = new jtextfield();     medicationtf = new jtextfield();     surgicalfeestf = new jtextfield();     labfeestf = new jtextfield();     rehabtf = new jtextfield();       totalchargesta = new jtextarea();      hospitalstaylabel.setsize(250, 30);     hospitalstaytf.setsize(200, 30);     medicationlabel.setsize(200, 30);     medicationtf.setsize(200, 30);     surgicalfeeslabel.setsize(200, 30);     surgicalfeestf.setsize(200, 30);     labfeeslabel.setsize(200, 30);     labfeestf.setsize(200, 30);     rehablabel.setsize(200, 30);     rehabtf.setsize(200,30);     totallabel.setsize(200, 30);     totalchargesta.setsize(200,30);     calculateb.setsize(100, 30);     exitb.setsize(100, 30);      hospitalstaylabel.setlocation(30, 25);     hospitalstaytf.setlocation(250, 25);     medicationlabel.setlocation(30, 60);     medicationtf.setlocation(250, 60);     surgicalfeeslabel.setlocation(30, 95);     surgicalfeestf.setlocation(250, 95);     labfeeslabel.setlocation(30, 130);     labfeestf.setlocation(250, 130);     rehablabel.setlocation(30, 165);     rehabtf.setlocation(250, 165);     totallabel.setlocation(30, 250);     totalchargesta.setlocation(250, 250);     calculateb.setlocation(70, 205);     exitb.setlocation(300, 205);      c.add(hospitalstaylabel);     c.add(hospitalstaytf);     c.add(medicationlabel);     c.add(medicationtf);     c.add(surgicalfeeslabel);     c.add(surgicalfeestf);     c.add(labfeeslabel);     c.add(labfeestf);     c.add(rehablabel);     c.add(rehabtf);     c.add(totallabel);     c.add(totalchargesta);     c.add(calculateb);     c.add(exitb);      hospitalstaytf.addactionlistener(this);     medicationtf.addactionlistener(this);     surgicalfeestf.addactionlistener(this);     labfeestf.addactionlistener(this);     rehabtf.addactionlistener(this);      setdefaultcloseoperation(exit_on_close);     setvisible(true);  }  public void actionperformedget(actionevent g) {     totaldays = integer.parseint(hospitalstaytf.gettext());     totalmedication = integer.parseint(medicationtf.gettext());     totalsurgical = integer.parseint(surgicalfeestf.gettext());     totallab = integer.parseint(labfeestf.gettext());     totalrehab = integer.parseint(rehabtf.gettext()); }  public int calcstaycharges() {     int dailycharge = 350;      totalstaycharge = totaldays * dailycharge;      return totalstaycharge; }  public int calcmisccharges() {     totalmisc = (totalmedication + totalsurgical + totallab + totalrehab);      return totalmisc; }  public int calctotalcharges() {     totalcharges = (totalstaycharge + totalmisc);      return totalcharges; }  public void actionperformed(actionevent e) {     if (e.getactioncommand().equals("calculate"))     {         totalchargesta.settext(string.valueof(totalcharges));     }     else if (e.getactioncommand().equals("exit"))         system.exit(0); }  public static void main(string[] args)  {     hospitalchargescalculator hospcalc = new hospitalchargescalculator(); } } 

i cant comment yet, here answer.

as said dont call function calculate total charges totalchargesta.settext(string.valueof(totalcharges)); instead try directly access variable totalcharges of method calctotalcharges() (the method should calling here), possible because declared all variables static int can access them in parts of code dont want access them.

dont declare variables static, , have seen error of not calling methods. think variables need static , reduce those.

also dont declare variables going use in just one function in class, inistead declare them in function, avoid same error static declarations.

-- also, additionaly answer provided subin thomas shows principal error in code, solution wont work because mixed of functions , have fix function:

public int calctotalcharges() {   totalcharges = (totalstaycharge + totalmisc);    return totalcharges; } 

where have same kind of error to:

public int calctotalcharges() {   totalcharges = (calcstaycharges() + calcmisccharges());    return totalcharges; } 

edit: have error in way retrieve values input textfields. why did use method public void actionperformedget(actionevent g) ?

this wont work actionlistener. instead copy code in function actionperformed method. work. completeness here working code:

import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class hospitalchargescalculator extends jframe implements actionlistener{  private jlabel hospitalstaylabel; private jlabel medicationlabel; private jlabel surgicalfeeslabel; private jlabel labfeeslabel; private jlabel rehablabel; private jlabel totallabel;  private jtextfield hospitalstaytf; private jtextfield medicationtf; private jtextfield surgicalfeestf; private jtextfield labfeestf; private jtextfield rehabtf;  private jtextarea totalchargesta;  private jbutton calculateb; private jbutton exitb;  public static final int width = 500; public static final int height = 350;   int totaldays; int totalmedication; int totalsurgical; int totallab; int totalrehab;  public hospitalchargescalculator() { settitle("hospital charges"); container c = getcontentpane(); setsize(width, height);  c.setlayout(null);  hospitalstaylabel = new jlabel(" number of days spent in hospital: ",         swingconstants.left); medicationlabel = new jlabel(" total medication charges: ",         swingconstants.left); surgicalfeeslabel = new jlabel(" total sugical charges : ",         swingconstants.left); labfeeslabel = new jlabel(" total lab fees: ",         swingconstants.left); rehablabel = new jlabel(" total rehab charges: ",         swingconstants.left); totallabel = new jlabel(" total charges: ",         swingconstants.left);  calculateb = new jbutton("calculate"); calculateb.addactionlistener(this); exitb = new jbutton("exit"); exitb.addactionlistener(this);   hospitalstaytf = new jtextfield(); medicationtf = new jtextfield(); surgicalfeestf = new jtextfield(); labfeestf = new jtextfield(); rehabtf = new jtextfield();   totalchargesta = new jtextarea();  hospitalstaylabel.setsize(250, 30); hospitalstaytf.setsize(200, 30); medicationlabel.setsize(200, 30); medicationtf.setsize(200, 30); surgicalfeeslabel.setsize(200, 30); surgicalfeestf.setsize(200, 30); labfeeslabel.setsize(200, 30); labfeestf.setsize(200, 30); rehablabel.setsize(200, 30); rehabtf.setsize(200,30); totallabel.setsize(200, 30); totalchargesta.setsize(200,30); calculateb.setsize(100, 30); exitb.setsize(100, 30);  hospitalstaylabel.setlocation(30, 25); hospitalstaytf.setlocation(250, 25); medicationlabel.setlocation(30, 60); medicationtf.setlocation(250, 60); surgicalfeeslabel.setlocation(30, 95); surgicalfeestf.setlocation(250, 95); labfeeslabel.setlocation(30, 130); labfeestf.setlocation(250, 130); rehablabel.setlocation(30, 165); rehabtf.setlocation(250, 165); totallabel.setlocation(30, 250); totalchargesta.setlocation(250, 250); calculateb.setlocation(70, 205); exitb.setlocation(300, 205);  c.add(hospitalstaylabel); c.add(hospitalstaytf); c.add(medicationlabel); c.add(medicationtf); c.add(surgicalfeeslabel); c.add(surgicalfeestf); c.add(labfeeslabel); c.add(labfeestf); c.add(rehablabel); c.add(rehabtf); c.add(totallabel); c.add(totalchargesta); c.add(calculateb); c.add(exitb);  hospitalstaytf.addactionlistener(this); medicationtf.addactionlistener(this); surgicalfeestf.addactionlistener(this); labfeestf.addactionlistener(this); rehabtf.addactionlistener(this);  setdefaultcloseoperation(exit_on_close); setvisible(true);  }   public int calcstaycharges() { int dailycharge = 350;  int totalstaycharge = totaldays * dailycharge;  return totalstaycharge; }  public int calcmisccharges() { int totalmisc = (totalmedication + totalsurgical + totallab + totalrehab);  return totalmisc; }  public int calctotalcharges() { int totalcharges = (calcstaycharges() + calcmisccharges());  return totalcharges; }  public void actionperformed(actionevent e) {  totaldays = integer.parseint(hospitalstaytf.gettext()); totalmedication = integer.parseint(medicationtf.gettext()); totalsurgical = integer.parseint(surgicalfeestf.gettext()); totallab = integer.parseint(labfeestf.gettext()); totalrehab = integer.parseint(rehabtf.gettext());  if (e.getactioncommand().equals("calculate")) {     totalchargesta.settext(calctotalcharges()+""); } else if (e.getactioncommand().equals("exit"))     system.exit(0); }  public static void main(string[] args)  { hospitalchargescalculator hospcalc = new hospitalchargescalculator(); } }   

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -