Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Need some help with my basic calculator

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Thanked 0 Times in 0 Posts

    Default Need some help with my basic calculator

    Hi all, if someone could help me out with my calculator I would really appreciate it.

    So the problem is, that it does the most basic of calculations of multiples, plus and minus, I haven't added division yet, that will come later. If I try 5 * 5 = 25 obviously, but if I try something like 5 * 5 * 5 = 25, it wipes the 1st 5 and calculates the 2nd and 3rd 5. Same thing goes for 5 + 5 + 5 = 10. I need it to be able to do equations like -3 + 5, 4 * 4 + 6, 3 * 2 - 5, etc etc.

    Thank you for your time everyone.

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Scanner;
    public class CalculatorPanel extends JPanel{
      private Calculator calc = new Calculator();
      //an array of buttons displayed on the calculator
      private JButton[] digitButtons;
      //output display for the calculator
      private JTextField display = new JTextField(10);
      //main method - sets up JFrame
      public static void main(String[]args){
        JFrame frame = new JFrame("Calculator");
        frame.setContentPane(new CalculatorPanel());
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      //constructor -- builds a GUI for a calculator
      public CalculatorPanel(){
        //create an array of button labels 
        String[] buttonLabels =  {"1", "2", "3", "4", "5", "6",
          "7", "8", "9", "C", "0", "=", "+", "-", "*"};
        //create an array of buttons
        digitButtons = new JButton[buttonLabels.length];
        //create an actionListener 
        ButtonListener  listener = new ButtonListener();
        // Create a 4 x 3 grid for placement of buttons. 
        JPanel buttonGrid = new JPanel();
        buttonGrid.setLayout(new GridLayout(5, 3));
        //create a button with each button label, add it to buttonGrid, and register the button as a listener
        for (int nextBut = 0; nextBut < digitButtons.length; nextBut++){
          digitButtons[nextBut] = new JButton(buttonLabels[nextBut]);
        //create a message for the user
        JLabel instruct = new JLabel("Press a button");
        //add the components to this JPanel
        setLayout(new BorderLayout());
        add(instruct, BorderLayout.NORTH);
        add(buttonGrid, BorderLayout.CENTER);
        add(display, BorderLayout.SOUTH);  
      //represents a listener for button presses 
      private class ButtonListener implements ActionListener{
        //what to do when a button has been pressed */
        public void actionPerformed(ActionEvent aE){
          JButton whichButton = (JButton) aE.getSource();
          display.setText(  whichButton.getText());
          if ("+".equals(whichButton.getText())){
          }else if ("-".equals(whichButton.getText())){
          }else if ("*".equals(whichButton.getText())){
          }else if ("=".equals(whichButton.getText())){
          }else if ("C".equals(whichButton.getText())){
            long i = 0;
            Scanner scan = new Scanner(whichButton.getText());
            i = scan.nextLong();

    public class Calculator{  
      private long currentInput;          //current input
      private long previousInput;         // previous input
      private long result;            // result of calculation
      private String lastOperator = "";  // keeps track of the last operator entered
      //new digit entered as integer value i - moves currentInput 1 decimal place to the left and adds i in "one's column"
      public void inDigit(long i){
        currentInput = (currentInput * 10) + i;
      //operator entered  + - or *  
      public void inOperator(String op){
        previousInput = currentInput;      // save the new input as previous to get ready for next input
        currentInput = 0;
        lastOperator = op;                 // remember which operator was entered
       //equals operation sets result to previousInput + - or * currentInput (depending on lastOperator)
      public void inEquals(){
        if (lastOperator.equals("+")){
          result = previousInput + currentInput;     
        } else if (lastOperator.equals("-")) { 
          result = previousInput - currentInput;
        } else if (lastOperator.equals("*"))  {
          result = previousInput * currentInput;
        lastOperator = "";       // reset last operator to "nothing"
      //clear operation
      public void inClear(){
        currentInput = 0;
        previousInput = 0;
        result = 0;
        lastOperator = "";
      //returns the current result
      public String getResult(){
        return Long.toString(result);  //converts int to String
      //returns the previous input value
      public String getPreviousInput(){
        return Long.toString(previousInput);
      //returns the current input value
      public String getCurrentInput(){
        return Long.toString(currentInput);
    Last edited by Synbios; October 7th, 2014 at 07:31 AM.

Similar Threads

  1. Very Basic Calculator
    By DreamHacked in forum Object Oriented Programming
    Replies: 7
    Last Post: June 12th, 2014, 09:17 AM
  2. simple basic calculator
    By kassie hans in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 16th, 2014, 04:42 AM
  3. Problem with basic calculator - Beginner
    By Dream Hacked in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 25th, 2013, 11:00 PM
  4. A basic calculator
    By SathApoc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 22nd, 2011, 12:47 PM
  5. Basic Calculator
    By Yes. in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 4th, 2010, 04:24 PM