1

The GUI on the calculator is finished however it does not do any math.

I heard about this command called "parse Int". With this I am trying to receive a string from the user and convert it into an integer. When I click an operator button I want it to receive input again and when i press the equals button I want java to register that string as the second number.

Does anyone know how I could approach this?

I am new in Java and I am doing this for experience with no time limit so I am open to different ways to make the calculator. Below I provide the code...

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.GridLayout;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.util.Scanner;
import javax.swing.JSplitPane;

public class CalcGUI {

private JFrame frame;
private JTextField textField;
private JTextField textField_1;

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

// parse (int)

/**
 * Create the application.
 */
public CalcGUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    frame.getContentPane().add(panel, BorderLayout.NORTH);
    panel.setLayout(new BorderLayout(0, 0));

    JSplitPane splitPane = new JSplitPane();
    panel.add(splitPane);

    textField = new JTextField();
    splitPane.setLeftComponent(textField);
    textField.setColumns(10);

 //a = int.parse(int);

    textField_1 = new JTextField();
    splitPane.setRightComponent(textField_1);
    textField_1.setColumns(10);

    JPanel panel_1 = new JPanel();
    frame.getContentPane().add(panel_1, BorderLayout.CENTER);
    panel_1.setLayout(new GridLayout(0, 3, 0, 0));

    JPanel panel_2 = new JPanel();
    panel_1.add(panel_2);

    JButton btnNewButton_01 = new JButton("1");
    panel_2.add(btnNewButton_01);
    btnNewButton_01.addActionListener(new ActionListener() {
          @Override
            public void actionPerformed(ActionEvent e) {
                    textField.setText(textField.getText() + "1");
          }});

    JButton btnNewButton_04 = new JButton("4");
    panel_2.add(btnNewButton_04);
    btnNewButton_04.addActionListener(new ActionListener() {

              @Override
                public void actionPerformed(ActionEvent e) {
                        textField.setText(textField.getText() + "4");
              }});

    JButton btnNewButton_07 = new JButton("7");
    panel_2.add(btnNewButton_07);
    btnNewButton_07.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                textField.setText(textField.getText() + "7");
      }}); 

    JButton btnNewButton_squared = new JButton("x^2");
    panel_2.add(btnNewButton_squared);
    btnNewButton_squared.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                textField.setText(textField.getText() + "^2");// NOT SURE YET
      }});

    JButton btnNewButton_root = new JButton("Sqr");
    panel_2.add(btnNewButton_root);
    btnNewButton_root.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            //do whatever should happen when the button is clicked...
          }
        });

    JButton btnNewButton_clear = new JButton("clear");
    btnNewButton_clear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    panel_2.add(btnNewButton_clear);

    JPanel panel_3 = new JPanel();
    panel_1.add(panel_3);

    JButton btnNewButton_02 = new JButton("2");
    panel_3.add(btnNewButton_02);
    btnNewButton_02.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                textField.setText(textField.getText() + "2");
      }});
    JButton btnNewButton_05 = new JButton("5");
    panel_3.add(btnNewButton_05);
    btnNewButton_05.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                textField.setText(textField.getText() + "5");
      }});

    JButton btnNewButton_08 = new JButton("8");
    panel_3.add(btnNewButton_08);
    btnNewButton_08.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                textField.setText(textField.getText() + "8");
      }});

    JButton btnNewButton_09 = new JButton("9");
    panel_3.add(btnNewButton_09);
    btnNewButton_09.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                                textField.setText(textField.getText() + "9");
      }});

    JButton btnNewButton_times = new JButton("x");
    panel_3.add(btnNewButton_times);
    btnNewButton_times.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                textField.setText(textField.getText() + "x");
      }});

    JButton btnNewButton_divide = new JButton("/");
    panel_3.add(btnNewButton_divide);
    btnNewButton_divide.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                textField.setText(textField.getText() + "/");
      }});

    JButton btnNewButton_delete = new JButton("delete");
    panel_3.add(btnNewButton_delete);
    btnNewButton_delete.addActionListener(new ActionListener() {

          public void actionPerformed(ActionEvent event) {
            //do whatever should happen when the button is clicked...
          }
        });

    JPanel panel_4 = new JPanel();
    panel_1.add(panel_4);

    JButton btnNewButton_03 = new JButton("3");
    btnNewButton_03.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                    textField.setText(textField.getText() + "3");
        }
    });
    panel_4.add(btnNewButton_03);

    JButton btnNewButton_06 = new JButton("6");
    btnNewButton_06.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    panel_4.add(btnNewButton_06);
    btnNewButton_06.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                textField.setText(textField.getText() + "6");
      }});

    JButton btnNewButton_00 = new JButton("0");
    panel_4.add(btnNewButton_00);
    btnNewButton_00.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                textField.setText(textField.getText() + "0");
      }});

    JButton btnNewButton_plus = new JButton("+");
    panel_4.add(btnNewButton_plus);
    btnNewButton_plus.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
                textField.setText(textField.getText() + "+");
      }});

    JButton btnNewButton_minus = new JButton("-");
    panel_4.add(btnNewButton_minus);
    btnNewButton_minus.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

                textField.setText(textField.getText() + "-");
      }});

    JButton btnNewButton_equals = new JButton("=");
    panel_4.add(btnNewButton_equals);
    btnNewButton_equals.addActionListener(new ActionListener() {

          public void actionPerformed(ActionEvent e) {
          }
    });
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tim2799
  • 71
  • 1
  • 1
  • 10
  • 1
    Did you try to solve it? It's better to show that you tried something before you decided to ask the question at SO. About `parseInt` - yes, you can do the following in Java: `int number = Integer.parseInt("100");`, it is used to convert `String` to `int`, so you can use it to convert your user input to number to work with. – coolguy Jan 03 '16 at 18:54
  • 1) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. 3) See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Jan 03 '16 at 19:08

1 Answers1

0

Copying the whole code into the question is a bit too much, but let me suggest you a hint as more or less pseudocode:

String numToConvert = textField.getText();
try {
    int num = new Integer(numToConvert);
    //sum up or ...
} catch (Exception e) { //numToConvertIsNotAnInteger
    //-> must be an operator
}
Alex7777
  • 18
  • 3