0

I am trying to build a calculator that works with JButtons for numbers and arithmetic operands. Every time a JButton is clicked, a String variable (textin) is updated and passed as the parameter of a non-Editable JTextField.The JTextField displays the number that is going to be passed in as a parameter for calculation.When an operand is clicked, the next number should reset the JTextField(i.e. "678+" when 4 is clicked the JTextField should reset to "4").The problem is that it does this EVERY time, regardless of the presence of "+".A fragment of the 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 is a plain button , whereas but6 is a button that should reset the JTextField only after a +.Unfortunately, it resets it EVERY time. I deleted the rest of the code for simplicity.The problem is probably in the if statement of the but6 JButton.Only what exists within else is executed.It doesn't actually check if "+" exists or not.Could someone explain why this happens?

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • 3
    We cant help you with a poor explaination of what is the problem in the code. You are failing to specifically explain the issue. Please update the question and be more specific with your problem. – Umer Farooq Aug 15 '13 at 16:58
  • Where is `tel` declared and initialized ? `textin` is a `String` variable. Try using `if (textin.equals("+")` instead of `==`. Here the contents of the two needs to be compared, not the location they referring to :-) – nIcE cOw Aug 15 '13 at 18:01
  • 1
    Wow it worked. I can't believe I forgot about .equals. Thanks a lot for taking the time to answer me, much appreciated! – Tolis Stefanidis Aug 15 '13 at 18:20
  • @TolisStefanidis : Please, you may also have a look at this way of creating a [calculator](http://stackoverflow.com/a/7441804/1057230), using [javax.script.ScriptEngine](http://docs.oracle.com/javase/7/docs/api/javax/script/ScriptEngine.html) package :-) – nIcE cOw Aug 16 '13 at 03:50
  • Never worked with Script Engine before, I'll check it out :) – Tolis Stefanidis Aug 16 '13 at 06:23

1 Answers1

0

Maybe your problem is that you are doing this: textin.substring(textin.length()-1)!="+") while you might need to do this !textin.substring(textin.length()-1).equals("+").
You should use the equals() method to check if they are the same.
Check this other question: Java String.equals versus ==.
Hope this helps.

Community
  • 1
  • 1
mikey
  • 1,339
  • 5
  • 22
  • 43