1

I am student practicing JAVA for few first times. We were asked to create a simple calculator. I've done everything without any error. My Only problem is that when I do a 1.66 + 55 for example or even 5 + 6 it shows that error in title. Here is the coede

import java.awt.BorderLayout;

public class Frame2 extends JFrame {

private JPanel contentPane;
private JTextField tot;
public String operateur = ""; 
public String s;
public String Value = "";
public String Value2 = "";
public Double Total = 0.0;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Frame2 frame = new Frame2();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Frame2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 267, 360);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    tot = new JTextField();
    tot.setFont(new Font("Tahoma", Font.PLAIN, 18));
    tot.setEditable(false);
    tot.setBounds(10, 11, 231, 45);
    contentPane.add(tot);
    tot.setColumns(10);

    JButton t7 = new JButton("7");
    t7.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 7;
            } else {
            Value2 = Value2.trim() + 7;
            }
            s = tot.getText();
            tot.setText(s+"7");
        }
    });
    t7.setBounds(10, 67, 49, 41);
    contentPane.add(t7);

    JButton t8 = new JButton("8");
    t8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if (operateur == "") { 
            Value = Value.trim() + 8;
            } else {
            Value2 = Value2.trim() + 8;
            }
            s = tot.getText();
            tot.setText(s+"8");


        }
    });
    t8.setBounds(69, 67, 50, 41);
    contentPane.add(t8);

    JButton t9 = new JButton("9");
    t9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 9;
            } else {
            Value2 = Value2.trim() + 9;
            }
            s = tot.getText();
            tot.setText(s+"9");
        }
    });
    t9.setBounds(129, 67, 50, 41);
    contentPane.add(t9);

    JButton t4 = new JButton("4");
    t4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 4;
            } else {
            Value2 = Value2.trim() + 4;
            }
            s = tot.getText();
            tot.setText(s+"4");
        }
    });
    t4.setBounds(10, 119, 50, 41);
    contentPane.add(t4);

    JButton t5 = new JButton("5");
    t5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 5;
            } else {
            Value2 = Value2.trim() + 5;
            }
            s = tot.getText();
            tot.setText(s+"5");
        }
    });
    t5.setBounds(69, 119, 50, 41);
    contentPane.add(t5);

    JButton t6 = new JButton("6");
    t6.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 6.0;
            } else {
            Value2 = Value2.trim() + 6.0;
            }
            s = tot.getText();
            tot.setText(s+"6");
        }
    });
    t6.setBounds(129, 119, 50, 41);
    contentPane.add(t6);

    JButton t3 = new JButton("3");
    t3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 3.0;
            } else {
            Value2 = Value2.trim() + 3.0;
            }
            s = tot.getText();
            tot.setText(s+"3");
        }
    });
    t3.setBounds(10, 173, 49, 39);
    contentPane.add(t3);

    JButton t2 = new JButton("2");
    t2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 2;
            } else {
            Value2 = Value2.trim() + 2;
            }
            s = tot.getText();
            tot.setText(s+"2");
        }
    });
    t2.setBounds(69, 171, 50, 41);
    contentPane.add(t2);

    JButton t1 = new JButton("1");

    t1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value + 1;
            } else if (operateur != null) {
            Value2 = Value2.trim() + 1;
            }
            s = tot.getText();
            tot.setText(s+"1");


        }
    });
    t1.setBounds(129, 171, 49, 41);
    contentPane.add(t1);

    JButton t0 = new JButton("0");
    t0.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 0;
            } else {
            Value2 = Value2.trim() + 0;
            }
            s = tot.getText();
            tot.setText(s+"0");
        }
    });
    t0.setBounds(129, 221, 50, 41);
    contentPane.add(t0);

    JButton virg = new JButton(",");
    virg.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
                Value = Value.trim() + ".";
                } else {
                Value2 = Value2.trim() + ".";
                }
                s = tot.getText();
                tot.setText(s+".");

        }
    });
    virg.setBounds(69, 223, 50, 39);
    contentPane.add(virg);

    JButton btnC = new JButton("C");
    btnC.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tot.setText("");
            operateur = "";
            Value = "";
            Value2 = "";
            Total = 0.0;

        }
    });
    btnC.setForeground(Color.RED);
    btnC.setBounds(10, 223, 49, 41);
    contentPane.add(btnC);

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="+";
            s = tot.getText();
            tot.setText(s +"+");



        }
    });
    plus.setBounds(188, 67, 53, 41);
    contentPane.add(plus);

    JButton calc = new JButton("CALCULER");
    calc.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if (operateur == "+") {
            Total = Double.valueOf(Value) + Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));

        }
        else if (operateur=="-") {
            Total = Double.valueOf(Value) - Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));
        }
        else if (operateur=="*") {
            Total = Double.valueOf(Value) * Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));
        }
        else if (operateur=="/") {
            Total = Double.valueOf(Value) / Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));
        }
            Value = String.valueOf(Total);
            Value2 = "";
            operateur = "";
            Total = 0.0;
        }
    });
    calc.setBounds(10, 273, 231, 38);
    contentPane.add(calc);

    JButton moins = new JButton("-");
    moins.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="-";
            s = tot.getText();
            tot.setText(s+"-");


        }
    });
    moins.setBounds(189, 119, 52, 41);
    contentPane.add(moins);

    JButton fois = new JButton("*");
    fois.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="*";
            s = tot.getText();
            tot.setText(s+"*");



        }
    });
    fois.setBounds(188, 173, 53, 39);
    contentPane.add(fois);

    JButton sur = new JButton("/");
    sur.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="/";
            s = tot.getText();
            tot.setText(s+"/");
        }
    });
    sur.setBounds(189, 221, 52, 41);
    contentPane.add(sur);
}

}

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Chaibi Alaa
  • 83
  • 2
  • 12
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. See [Why is it frowned upon to use a null layout in SWING?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) for more details – MadProgrammer Oct 01 '14 at 23:39
  • `String` comparison in Java is done with the `String.equals` method and not `==`...`operateur == ""` should be `"".equals(operateur)`... – MadProgrammer Oct 01 '14 at 23:40
  • You're going to need to put some debug information into your code and/or use the debugger to check what's going on. When I look at it, `Value` is equal to `1.6.06.0` when the calculation is attempted... – MadProgrammer Oct 01 '14 at 23:43

1 Answers1

5

So, you have a multitidue of problems which are likely to come back and bite you, but lets look at the immediate problem...

public void actionPerformed(ActionEvent e) {
    if (operateur == "") { 
        Value = Value.trim() + 6.0;
    } else {
        Value2 = Value2.trim() + 6.0;
    }
    s = tot.getText();
    tot.setText(s+"6");
}

Each time you press 6, it is appending 6.0 to the end of the String, so when you type 1.66, you are actually getting 1.6.06.0...which obviously isn't what you want.

Change + 6.0 to + 6...and do the same for + 3.0...

Also take a look at How do I compare strings in Java? and Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366