0

Here is my full code --->>>>

package calculator;

import java.awt.Font;
import java.awt.GridLayout;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CalField extends JPanel {
    private JTextField field;
    private String track = null;
    private String s = "";
    private int i = 0;

    public CalField() {

        setLayout(new GridLayout(1, 1, 5, 5));
        field = new JTextField(20);
        field.setFont(new Font("SansSerif", Font.PLAIN, 20));
        field.setEditable(true);
        add(field);

        s = new String[50];
    }

    public JTextField getField() {
        return field;
    }

    public void appendField(String text) {
        if (field.getText().length() < 16) {
            if (text == "+" || text == "-" || text == "*" || text == "/"
                    || text == "(" || text == ")") {
                track = null;  // track for keep track of operator
                if (track == null) {
                    track = text;
                    s = "";
                }

                field.setText(field.getText() + text);
            } else if (field.getText().contains(".")) { // if JTextField's
                                                        // string
                if (track != null) {                    // contains a "." then
                    if (text != ".") {                   // check
                        field.setText(field.getText() + text); // here
                        s.concat(text);  //here is also problem
                    } else if (text == ".") {

                        if (!s.contains(".")) //here is also problem
                            field.setText(field.getText() + text);
                        else
                            field.setText(field.getText() + "");
                    }
                }

                else {

                    if (text == ".")
                        field.setText(field.getText() + "");
                    else
                        field.setText(field.getText() + text);
                }
            } else
                field.setText(field.getText() + text); // if not contains "."
        }
        System.out.print(text);
    }

    public void calculate() {
        Object result = "";
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        try {
            result = engine.eval(field.getText());
        } catch (ScriptException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String res = "";
        res = String.valueOf(result);
        field.setText(res);
    }

    public void setTrack(String track) {
        this.track = track;
    }
}
package calculator;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class CalButton extends JPanel {
    private JButton[] buttons;

    private static final String[] buttonNames = { "7", "8", "9", "/", "4", "5", "6",
            "*", "1", "2", "3", "-", "0", ".", "=", "+" };
    private CalField calField;

    public CalButton(CalField calField) {
        buttons = new JButton[buttonNames.length];
        setLayout(new GridLayout(4, 4, 3, 3));
        this.calField = calField;
        ButtonListener handler = new ButtonListener();
        for (int i = 0; i < buttonNames.length; i++) {
            buttons[i] = new JButton(buttonNames[i]);
            buttons[i].setFont(new Font("SansSerif", Font.BOLD, 20));

            add(buttons[i]);
            buttons[i].addActionListener(handler);
        }

    }

    private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            // TODO Auto-generated method stub

            if (buttons[0].getActionCommand() == event.getActionCommand()) {
                calField.appendField(event.getActionCommand());
                // System.out.print(buttons[0].getActionCommand());
            } else if (buttons[1].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[1].getActionCommand());
            } else if (buttons[2].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[2].getActionCommand());
            } else if (buttons[3].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[3].getActionCommand());
            } else if (buttons[4].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[4].getActionCommand());
            } else if (buttons[5].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[5].getActionCommand());
            } else if (buttons[6].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[6].getActionCommand());
            } else if (buttons[7].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[7].getActionCommand());
            } else if (buttons[8].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[8].getActionCommand());
            } else if (buttons[9].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[9].getActionCommand());
            } else if (buttons[10].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[10].getActionCommand());
            } else if (buttons[11].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[11].getActionCommand());
            } else if (buttons[12].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[12].getActionCommand());
            } else if (buttons[13].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[13].getActionCommand());
            } else if (buttons[14].getActionCommand() == event
                    .getActionCommand()) {
                // calField.getField().setText(String.valueOf(calField.eval(calField.getField().getText())));
                calField.setTrack(null);
                calField.calculate();
            } else if (buttons[15].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[15].getActionCommand());
            }
        }
    }
}
package calculator;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class BackIcon extends JPanel {
    private Icon backIcon = new ImageIcon(getClass().getResource(
            "calculatorData/Back-24.png"));
    private JButton button;
    private CalField calField;

    public BackIcon(CalField calField) {
        setLayout(new BorderLayout());
        button = new JButton(backIcon);
        button.setSize(50, 50);
        add(button, BorderLayout.EAST);
        button.addActionListener(new BackButtonHandler());
        this.calField = calField;
    }

    private class BackButtonHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            String str = "";
            try {
                str = calField.getField().getText();

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

            if (str != null) {
                str = str.substring(0, str.length() - 1);
                calField.getField().setText(str);
            }
        }

    }
}

--> main class--->>>

package calculator;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Calculator {
    public static void main(String[] args) {

        JFrame application = new JFrame("Calculator");
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(250, 255);
        application.setResizable(false);

        CalField calField = new CalField();
        BackIcon backIcon = new BackIcon(calField);
        CalButton calButton = new CalButton(calField);

        application.add(calField, BorderLayout.PAGE_START);
        application.add(backIcon, BorderLayout.CENTER);
        application.add(calButton, BorderLayout.PAGE_END);

        application.setVisible(true);
    }
}

In the CalField class's appendField() method, I've got a problem. Here I want to take only one dot(.) for each decimal(like 44.5) number. But in this program, this occurs only for my first input of decimal number. When I input any operator(like +,-,*,/) ,it can take more than one dots ( like 44.......5). any one please help me to solve this problem.

smk pobon
  • 113
  • 1
  • 12
  • Note, this will fail: `if (text == ".")`. Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead use `if (text.equals("."))` – Hovercraft Full Of Eels Oct 24 '15 at 18:47
  • Thanks @HovercraftFullOfEels. But my problem is'nt solved yet. – smk pobon Oct 24 '15 at 19:03
  • SomeOne help me ..... I didn't get my answer... – smk pobon Oct 25 '15 at 04:50

0 Answers0