swing - Getting last character of a JTextField(or its String) in Java -


i trying build calculator works jbuttons numbers , arithmetic operands. every time jbutton clicked, string variable (textin) updated , passed parameter of non-editable jtextfield.the jtextfield displays number going passed in parameter calculation.when operand clicked, next number should reset jtextfield(i.e. "678+" when 4 clicked jtextfield should reset "4").the problem every time, regardless of presence of "+".a fragment of code follows.

import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class gui extends jframe {      private jbutton but1;      private jbutton but6;     private jbutton plus;     private jbutton equal;     private jtextfield text;     public static string textin;     public double num1;     public double num2;     public string operand;      public gui() {         super("calculator");         setlayout(new flowlayout());          textin = "";         num1 = 0;         num2 = 0;         but1 = new jbutton("1");         but6 = new jbutton("6");         plus = new jbutton("+");         equal = new jbutton("=");         operand = "";          text = new jtextfield(20);         text.seteditable(false);         add(text);         add(but6);         add(but1);         add(equal);         add(plus);          but1.addactionlistener(new actionlistener() {             public void actionperformed(actionevent event) {                 textin += "1";                 text.settext(textin);             }         });          but6.addactionlistener(new actionlistener() {             public void actionperformed(actionevent event) {                 if(textin.length()!=0&&textin.substring(textin.length()-1)!="+"){                     textin += "6";                     text.settext(textin);                     textin = "6";                     text.settext(textin);                 } else {                     textin = "6";                     text.settext(textin);                 }             }         });          plus.addactionlistener(new actionlistener() {             public void actionperformed(actionevent event) {                 num2 = double.parsedouble(textin);                 num1 = num1 + num2;                 textin += "+";                 text.settext(textin);                  operand = "+";             }         });          equal.addactionlistener(new actionlistener() {             public void actionperformed(actionevent event) {                 if(tel == "+") {                     num2 = double.parsedouble(textin);                     num1 = num1+num2;                     joptionpane.showmessagedialog(null,string.format("%f",num1));                 } else {                     joptionpane.showmessagedialog(null,string.format("error"));                 }             }         });     } } 

but1 plain button , whereas but6 button should reset jtextfield after +.unfortunately, resets every time. deleted rest of code simplicity.the problem in if statement of but6 jbutton.only exists within else executed.it doesn't check if "+" exists or not.could explain why happens?

maybe problem doing this: textin.substring(textin.length()-1)!="+") while might need !textin.substring(textin.length()-1).equals("+").
should use equals() method check if same.
check other question: java string.equals versus ==.
hope helps.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -