java - Use two digits from the same jTextfield to perform a calculation -
i have completed basic calculator part of assignment. using 2 text feild enter opperands. , press operator display answer.
however, both integers supposed entered same textfield, how do that?
i have considered using array or stack, could'nt work.
i included code below... calculator ready, , works well, multiplication division addition , subtraction, , displays answer in text area, 2 numbers added or subtracted... typed 2 seperate fields, once pressing opperand calculation done (reverse polish notation), however, how can eliminate use of 2nd field, , enter first value textfield, press enter insert second value same text field , pressing enter. saving first vala , second val b (integers). has same textfield.
/* * change template, choose tools | templates * , open template in editor. */ /* * calcu.java * * created on feb 9, 2011, 10:11:37 pm */ /** * * @author halaseh */ public class calcu extends javax.swing.jframe { /** creates new form calcu */ public calcu() { initcomponents(); } /** method called within constructor * initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { jscrollpane1 = new javax.swing.jscrollpane(); jtextarea1 = new javax.swing.jtextarea(); jtextfield1 = new javax.swing.jtextfield(); jtextfield2 = new javax.swing.jtextfield(); jbutton1 = new javax.swing.jbutton(); jbutton2 = new javax.swing.jbutton(); jbutton3 = new javax.swing.jbutton(); jbutton4 = new javax.swing.jbutton(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); jtextarea1.setcolumns(20); jtextarea1.setrows(5); jscrollpane1.setviewportview(jtextarea1); jtextfield1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jtextfield1actionperformed(evt); } }); jtextfield2.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jtextfield2actionperformed(evt); } }); jbutton1.settext("+"); jbutton1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jbutton1actionperformed(evt); } }); jbutton2.settext("-"); jbutton2.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jbutton2actionperformed(evt); } }); jbutton3.settext("*"); jbutton3.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jbutton3actionperformed(evt); } }); jbutton4.settext("/"); jbutton4.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jbutton4actionperformed(evt); } }); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing, false) .addcomponent(jtextfield1, javax.swing.grouplayout.alignment.leading) .addcomponent(jtextfield2, javax.swing.grouplayout.alignment.leading) .addgroup(javax.swing.grouplayout.alignment.leading, layout.createsequentialgroup() .addcomponent(jbutton3) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jbutton4, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)) .addgroup(javax.swing.grouplayout.alignment.leading, layout.createsequentialgroup() .addcomponent(jbutton1) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jbutton2)))) .addcontainergap(222, short.max_value)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jtextfield1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jtextfield2, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jbutton1) .addcomponent(jbutton2)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jbutton3) .addcomponent(jbutton4)) .addcontainergap(69, short.max_value)) ); pack(); }// </editor-fold> private void jbutton1actionperformed(java.awt.event.actionevent evt) { int vala = integer.parseint(jtextfield1.gettext()); int valb = integer.parseint(jtextfield2.gettext()); // todo add handling code here: int valc = vala+valb; jtextarea1.append(integer.tostring(valc)); jtextarea1.append("\n"); } private void jtextfield1actionperformed(java.awt.event.actionevent evt) { int vala = integer.parseint(jtextfield1.gettext()); } private void jtextfield2actionperformed(java.awt.event.actionevent evt) { int valb = integer.parseint(jtextfield2.gettext()); // todo add handling code here: } private void jbutton2actionperformed(java.awt.event.actionevent evt) { int vala = integer.parseint(jtextfield1.gettext()); int valb = integer.parseint(jtextfield2.gettext()); // todo add handling code here: int valc = vala-valb; jtextarea1.append(integer.tostring(valc)); jtextarea1.append("\n"); // todo add handling code here: } private void jbutton3actionperformed(java.awt.event.actionevent evt) { int vala = integer.parseint(jtextfield1.gettext()); int valb = integer.parseint(jtextfield2.gettext()); // todo add handling code here: int valc = vala*valb; jtextarea1.append(integer.tostring(valc)); jtextarea1.append("\n"); // todo add handling code here: } private void jbutton4actionperformed(java.awt.event.actionevent evt) { int vala = integer.parseint(jtextfield1.gettext()); int valb = integer.parseint(jtextfield2.gettext()); // todo add handling code here: int valc = vala/valb; jtextarea1.append(integer.tostring(valc)); jtextarea1.append("\n"); // todo add handling code here: } /** * @param args command line arguments */ public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { public void run() { new calcu().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jbutton jbutton1; private javax.swing.jbutton jbutton2; private javax.swing.jbutton jbutton3; private javax.swing.jbutton jbutton4; private javax.swing.jscrollpane jscrollpane1; private javax.swing.jtextarea jtextarea1; private javax.swing.jtextfield jtextfield1; private javax.swing.jtextfield jtextfield2; // end of variables declaration }
you have read in input , parse string in way.
if have expect things of form 1+3, or 4*5, etc, can check operation asking whether there exists of operator symbols
if(inputstring.indexof("+") >= 0) { //handle addition }
and both of numbers using:
string[] numbers = inputstring.split("+"); integer numberone = integer.parseint(numbers[0]); ...
this makes lot of assumptions, , in general should check potential problems, depends on how project specified.
Comments
Post a Comment