Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Help with calculator program(Need help with a calculator java program that does not need WindowBuiler nor Design)
Help with calculator program [message #1821696] Tue, 18 February 2020 19:06 Go to next message
Christian Soares is currently offline Christian SoaresFriend
Messages: 2
Registered: February 2020
Junior Member
Greetings, Eclipse Community.

I'm struggling with making a Java calculator. I can't figure out how to make my buttons respond to calculations, such as pressing one button and then an arithmetic button (sum, minus, multiply, divide, equals) so that when I press a different button (or the same one), it gives me the calculated result. How do I do this?
I also have to organize the calculator with grids and panels, but I'll leave it for last. For now, I need advice on how I can make my buttons respond like a calculator. Here's my full code.

import java.awt.*;
import java.awt.event.*;
import java.awt.peer.ComponentPeer;
import java.util.EventObject;

import javax.swing.*;
@SuppressWarnings("serial")

public class Calculator extends JFrame implements ActionListener
{
private JLabel myTitle;
private Font myTitleFont;
private JButton FClear;
private JButton Btn1, Btn2, Btn3, Btn4, Btn5, Btn6, Btn7, Btn8, Btn9;
private JButton FSum, FMinus, FMulti, FDivide, FComma, FEquals;
private JPanel myPanel;
private JTextField result;

public static void main(String[] args)
{
new Calculator();
}
public Calculator()
{
setSize(500,500);
setTitle("Calculator");
this.setResizable(false);

myTitle = new JLabel ("Calculator");
myTitleFont = new Font(Font.MONOSPACED, Font.PLAIN,40);

myPanel = new JPanel();
myPanel.setLayout(new FlowLayout());
myPanel.setPreferredSize (new Dimension (500,500));

result = new JTextField(20);
FClear = new JButton ("Calc");
Btn1 = new JButton ("1");
Btn2 = new JButton("2");
Btn3 = new JButton("3");
Btn4 = new JButton("4");
Btn5 = new JButton("5");
Btn6 = new JButton("6");
Btn7 = new JButton("7");
Btn8 = new JButton("8");
Btn9 = new JButton("9");
FSum = new JButton ("+");
FMinus = new JButton ("-");
FMulti = new JButton ("*");
FDivide = new JButton ("/");
FComma = new JButton (",");
FEquals = new JButton ("=");

FClear.addActionListener(this);
Btn1.addActionListener(this);
Btn2.addActionListener(this);
Btn3.addActionListener(this);
Btn4.addActionListener(this);
Btn5.addActionListener(this);
Btn6.addActionListener(this);
Btn7.addActionListener(this);
Btn8.addActionListener(this);
Btn9.addActionListener(this);
FSum.addActionListener(this);
FMinus.addActionListener(this);
FMulti.addActionListener(this);
FDivide.addActionListener(this);
FComma.addActionListener(this);
FEquals.addActionListener(this);

myPanel.add(myTitle);
myPanel.add(result);
myPanel.add(FClear);
myPanel.add(Btn1);
myPanel.add(Btn2);
myPanel.add(Btn3);
myPanel.add(Btn4);
myPanel.add(Btn5);
myPanel.add(Btn6);
myPanel.add(Btn7);
myPanel.add(Btn8);
myPanel.add(Btn9);
myPanel.add(FSum);
myPanel.add(FMinus);
myPanel.add(FMulti);
myPanel.add(FDivide);
myPanel.add(FComma);
myPanel.add(FEquals);

this.getContentPane().add(myPanel);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == FClear)
{
result.setText("");
}
if(e.getSource() == Btn1)
{
String prev = result.getText();
result.setText(prev+"1");

}
if(e.getSource() == Btn2)
{
String prev = result.getText();
result.setText(prev+"2");
}
if(e.getSource() == Btn3)
{
String prev = result.getText();
result.setText(prev+"3");
}
if(e.getSource() == Btn4)
{
String prev = result.getText();
result.setText(prev+"4");
}
if(e.getSource() == Btn5)
{
String prev = result.getText();
result.setText(prev+"5");
}
if(e.getSource() == Btn6)
{
String prev = result.getText();
result.setText(prev+"6");
}
if(e.getSource() == Btn7)
{
String prev = result.getText();
result.setText(prev+"7");
}
if(e.getSource() == Btn8)
{
String prev = result.getText();
result.setText(prev+"8");
}
if(e.getSource() == Btn9)
{
String prev = result.getText();
result.setText(prev+"9");
}
}
}
Re: Help with calculator program [message #1821710 is a reply to message #1821696] Wed, 19 February 2020 00:17 Go to previous messageGo to next message
Christian Soares is currently offline Christian SoaresFriend
Messages: 2
Registered: February 2020
Junior Member
UPDATE:

I have tried to make the calculations, but I ran into an error where the equals is not responding properly. Here is my code as of tonight:

import java.awt.*;
import java.awt.event.*;
import java.awt.peer.ComponentPeer;
import java.util.EventObject;

import javax.swing.*;
@SuppressWarnings("serial")

public class Calculator extends JFrame implements ActionListener
{
private JLabel myTitle;
private Font myTitleFont;
private JButton FClear;
private JButton Btn1, Btn2, Btn3, Btn4, Btn5, Btn6, Btn7, Btn8, Btn9;
private JButton FSum, FMinus, FMulti, FDivide, FComma, FEquals;
private JPanel myPanel;
private static JTextField result;
static double numb;
static double answer;
static int calculate;

public static void mathmematical_operation()
{
switch(calculate)
{
case 1:
answer = numb + Double.parseDouble(result.getText());
result.setText(Double.toString(answer));
break;

case 2:
answer = numb - Double.parseDouble(result.getText());
result.setText(Double.toString(answer));
break;

case 3:
answer = numb * Double.parseDouble(result.getText());
result.setText(Double.toString(answer));
break;

case 4:
answer = numb / Double.parseDouble(result.getText());
result.setText(Double.toString(answer));
break;
}
}
public static void main(String[] args)
{
new Calculator();
}
public Calculator()
{
setSize(500,500);
setTitle("Calculator");
this.setResizable(false);

myTitle = new JLabel ("Calculator");
myTitleFont = new Font(Font.MONOSPACED, Font.PLAIN,40);

myPanel = new JPanel();
myPanel.setLayout(new FlowLayout());
myPanel.setPreferredSize (new Dimension (500,500));

result = new JTextField(20);
FClear = new JButton ("Calc");
Btn1 = new JButton ("1");
Btn2 = new JButton("2");
Btn3 = new JButton("3");
Btn4 = new JButton("4");
Btn5 = new JButton("5");
Btn6 = new JButton("6");
Btn7 = new JButton("7");
Btn8 = new JButton("8");
Btn9 = new JButton("9");
FSum = new JButton ("+");
FMinus = new JButton ("-");
FMulti = new JButton ("*");
FDivide = new JButton ("/");
FComma = new JButton (",");
FEquals = new JButton ("=");

FClear.addActionListener(this);
Btn1.addActionListener(this);
Btn2.addActionListener(this);
Btn3.addActionListener(this);
Btn4.addActionListener(this);
Btn5.addActionListener(this);
Btn6.addActionListener(this);
Btn7.addActionListener(this);
Btn8.addActionListener(this);
Btn9.addActionListener(this);
FSum.addActionListener(this);
FMinus.addActionListener(this);
FMulti.addActionListener(this);
FDivide.addActionListener(this);
FComma.addActionListener(this);
FEquals.addActionListener(this);

myPanel.add(myTitle);
myPanel.add(result);
myPanel.add(FClear);
myPanel.add(Btn1);
myPanel.add(Btn2);
myPanel.add(Btn3);
myPanel.add(Btn4);
myPanel.add(Btn5);
myPanel.add(Btn6);
myPanel.add(Btn7);
myPanel.add(Btn8);
myPanel.add(Btn9);
myPanel.add(FSum);
myPanel.add(FMinus);
myPanel.add(FMulti);
myPanel.add(FDivide);
myPanel.add(FComma);
myPanel.add(FEquals);

this.getContentPane().add(myPanel);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == FClear)
{
result.setText("");
}
if(e.getSource() == Btn1)
{
String prev = result.getText();
result.setText(prev+"1");
}
if(e.getSource() == Btn2)
{
String prev = result.getText();
result.setText(prev+"2");
}
if(e.getSource() == Btn3)
{
String prev = result.getText();
result.setText(prev+"3");
}
if(e.getSource() == Btn4)
{
String prev = result.getText();
result.setText(prev+"4");
}
if(e.getSource() == Btn5)
{
String prev = result.getText();
result.setText(prev+"5");
}
if(e.getSource() == Btn6)
{
String prev = result.getText();
result.setText(prev+"6");
}
if(e.getSource() == Btn7)
{
String prev = result.getText();
result.setText(prev+"7");
}
if(e.getSource() == Btn8)
{
String prev = result.getText();
result.setText(prev+"8");
}
if(e.getSource() == Btn9)
{
String prev = result.getText();
result.setText(prev+"9");
}
if(e.getSource() == FSum)
{
numb = Double.parseDouble(result.getText());
calculate = 1;
result.setText("");
result.setText(numb + "+");
}
if(e.getSource() == FMinus)
{
numb = Double.parseDouble(result.getText());
calculate = 2;
result.setText("");
result.setText(numb + "-");
}
if(e.getSource() == FMulti)
{
numb = Double.parseDouble(result.getText());
calculate = 3;
result.setText("");
result.setText(numb + "*");
}
if(e.getSource() == FDivide)
{
numb = Double.parseDouble(result.getText());
calculate = 4;
result.setText("");
result.setText(numb + "/");
}
if(e.getSource() == FEquals)
{
mathmematical_operation();
result.setText("");

}
if(e.getSource() == FComma)
{
String prev = result.getText();
result.setText(prev+",");
}
}
}

Whenever I press the equals, it gives me the following error log:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "9.0+9"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at Calculator.mathmematical_operation(Calculator.java:27)
at Calculator.actionPerformed(Calculator.java:206)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Re: Help with calculator program [message #1821721 is a reply to message #1821710] Wed, 19 February 2020 07:01 Go to previous messageGo to next message
Eitan Rosenberg is currently offline Eitan RosenbergFriend
Messages: 58
Registered: October 2018
Member
Hello there.

You need some kind of "java Algebra equation parser" to do the task.

Google....

Re: Help with calculator program [message #1821731 is a reply to message #1821721] Wed, 19 February 2020 09:11 Go to previous message
Eitan Rosenberg is currently offline Eitan RosenbergFriend
Messages: 58
Registered: October 2018
Member
Hello there again.

If it is OK for you to use external library have a look at :

https://en.wikipedia.org/wiki/Mxparser

<dependency>
<groupId>org.mariuszgromada.math</groupId>
<artifactId>MathParser.org-mXparser</artifactId>
<version>4.4.2</version>
</dependency>


Previous Topic:How to install Plugin in eclipse effectively
Next Topic:Mark code blocks in different colours ?
Goto Forum:
  


Current Time: Sun Oct 25 07:38:02 GMT 2020

Powered by FUDForum. Page generated in 0.02199 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top