Please help with calculator program
I'm trying to input multiple numbers but it only solves two numbers...
public class Calculator extends JFrame{
private JPanel panel;
private JTextField display;
private JButton[] button;
static String lastCommand = null;
double res;
int tmp=0;
boolean bool=false;
private final String[] btn = {"1","2","3","4","5","6","7","8","9","0","C","+ ","-","*","/","="};
public Calculator() {
GridLayout gridLayout = new GridLayout(4,4,5,5);
panel = new JPanel(gridLayout);
display = new JTextField("0");
display.setFont(new Font("Arial",Font.BOLD,40));
display.setHorizontalAlignment(JTextField.RIGHT);
display.setEditable(false);
add(display,BorderLayout.NORTH);
add(panel, BorderLayout.SOUTH);
button = new JButton[16];
HandlerClass handler = new HandlerClass();
for(int i=0;i<btn.length;i++)
{
button[i] = new JButton(btn[i]);
button[i].setForeground(Color.red);
button[i].setFont(new Font("Arial",Font.BOLD,30));
panel.add(button[i]);
button[i].addActionListener(handler);
}
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent e) {
String clickBtn = e.getActionCommand();
if(clickBtn.equalsIgnoreCase("C"))
{
display.setText("0");
}
else if(clickBtn.equals("+"))
{
lastCommand="+";
tmp = Integer.parseInt(display.getText());
bool = true;
}
else if(clickBtn.equals("-"))
{
lastCommand="-";
tmp = Integer.parseInt(display.getText());
bool = true;
}
else if(clickBtn.equals("*"))
{
lastCommand="*";
tmp = Integer.parseInt(display.getText());
bool = true;
}
else if(clickBtn.equals("/"))
{
lastCommand="/";
tmp = Integer.parseInt(display.getText());
display.setText("0");
}
else if (clickBtn.equalsIgnoreCase("="))
{
double num2 = Integer.parseInt(display.getText());
if(lastCommand.equals("+"))
{
display.setText(String.valueOf(num2+tmp));
res=num2+tmp;
}
else if(lastCommand.equals("-"))
{
display.setText(String.valueOf(tmp-num2));
res=tmp-num2;
}
else if(lastCommand.equals("*"))
{
display.setText(String.valueOf(num2*tmp));
res=num2*tmp;
}
else if(lastCommand.equals("/"))
{
try{
display.setText(String.valueOf(tmp/num2));
if(num2 == 0)
{
display.setText("Error");
}
res=tmp/num2;
}catch(ArithmeticException x)
{
display.setText("");
}
}
}
else
{
if(display.getText().equals("0") || bool==true)
{
display.setText("");
bool = false;
}
display.setText(display.getText()+e.getActionComma nd());
}
}
}
}
Please help thanks
Re: Please help with calculator program
Quote:
Originally Posted by
dipizamora
This is the code... I'm trying to input multiple numbers but it only solves two numbers...
Can you explain this in more detail? Explain it as if we don't have a clue as to anything about your program or how it works.
You'll want to edit your post above and wrap the code in [code] [/code] tags so that it retains its formatting and is readable. Note the difference?:
Code :
public class Calculator extends JFrame{
private JPanel panel;
private JTextField display;
private JButton[] button;
static String lastCommand = null;
double res;
int tmp=0;
boolean bool=false;
private final String[] btn = {"1","2","3","4","5","6","7","8","9","0","C","+","-","*","/","="};
public Calculator() {
GridLayout gridLayout = new GridLayout(4,4,5,5);
panel = new JPanel(gridLayout);
display = new JTextField("0");
display.setFont(new Font("Arial",Font.BOLD,40));
display.setHorizontalAlignment(JTextField.RIGHT);
display.setEditable(false);
add(display,BorderLayout.NORTH);
add(panel, BorderLayout.SOUTH);
button = new JButton[16];
HandlerClass handler = new HandlerClass();
for(int i=0;i<btn.length;i++)
{
button[i] = new JButton(btn[i]);
button[i].setForeground(Color.red);
button[i].setFont(new Font("Arial",Font.BOLD,30));
panel.add(button[i]);
button[i].addActionListener(handler);
}
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent e) {
String clickBtn = e.getActionCommand();
if(clickBtn.equalsIgnoreCase("C"))
{
display.setText("0");
}
else if(clickBtn.equals("+"))
{
lastCommand="+";
tmp = Integer.parseInt(display.getText());
bool = true;
}
else if(clickBtn.equals("-"))
{
lastCommand="-";
tmp = Integer.parseInt(display.getText());
bool = true;
}
else if(clickBtn.equals("*"))
{
lastCommand="*";
tmp = Integer.parseInt(display.getText());
bool = true;
}
else if(clickBtn.equals("/"))
{
lastCommand="/";
tmp = Integer.parseInt(display.getText());
display.setText("0");
}
else if (clickBtn.equalsIgnoreCase("="))
{
double num2 = Integer.parseInt(display.getText());
if(lastCommand.equals("+"))
{
display.setText(String.valueOf(num2+tmp));
res=num2+tmp;
}
else if(lastCommand.equals("-"))
{
display.setText(String.valueOf(tmp-num2));
res=tmp-num2;
}
else if(lastCommand.equals("*"))
{
display.setText(String.valueOf(num2*tmp));
res=num2*tmp;
}
else if(lastCommand.equals("/"))
{
try{
display.setText(String.valueOf(tmp/num2));
if(num2 == 0)
{
display.setText("Error");
}
res=tmp/num2;
}catch(ArithmeticException x)
{
display.setText("");
}
}
}
else
{
if(display.getText().equals("0") || bool==true)
{
display.setText("");
bool = false;
}
display.setText(display.getText()+e.getActionCommand());
}
}
}
}
Re: Please help with calculator program
I'm sorry. I'm just new here. It's confusing. Anyway, the problem with the program is that we are trying to solve multiple mathematical operations to input numbers but it only solves the first two numbers. For example: 2+2+2+2+2+2 = 4. That's the problem.
Re: Please help with calculator program
Which part of your code was written to handle the third number?
Re: Please help with calculator program
Can you edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting as shown in post#2
Re: Please help with calculator program
Quote:
Originally Posted by
jps
Which part of your code was written to handle the third number?
And which parts were written by you vs which parts were borrowed from online sources? It's important for us to know which part of the code you fully understand and which parts you might not understand as well.
Re: Please help with calculator program
Well, for addition shouldn't it be
Code :
tmp += Integer.parseInt(display.getText());
instead of
Code :
tmp = Integer.parseInt(display.getText());
? Just had a quick look.