So, right now I'm trying to build a calculator in Java, I have the GUI built, but I cannot find a way to take the text from the pane and do that necessary math on it. Example The text pane says
1 + 2
I want to be able to press a button and calculates it and do the math and display it as
1 + 2 = 3
I've tried taking the text and using the parseInt, parseFloat, and parseDouble. Every time I try to take the text and turn it into an Int, Double, or Float I get an error. The error says
"Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1 + 1""
Any ideas on what I could to make it work
Code :
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
//General Information: SOL is a
//Sorry for what I'm sure is sloppy code I'm new to programming
public class MathDoer_Main {
//initializing all of the frames, the text pane, key listeners, the ScrollPane, and all of the buttons
public static JFrame mathDoerFrame = new JFrame("mathDoer");
public static JTextPane mathDoerText = new JTextPane();
public static JScrollPane mathDoerTextScoll = new JScrollPane();
public static JTextPane mathDoerConsole = new JTextPane();
public static JScrollPane mathDoerConsoleScroll = new JScrollPane();
public static JButton mathDoer0Button;
public static JButton mathDoerClearButton = new JButton("Clear");
public static JButton mathDoerDecimalButton;
public static JButton mathDoer1Button;
public static JButton mathDoer2Button;
public static JButton mathDoer3Button;
public static JButton mathDoer4Button;
public static JButton mathDoer5Button;
public static JButton mathDoer6Button;
public static JButton mathDoer7Button;
public static JButton mathDoer8Button;
public static JButton mathDoer9Button;
public static JButton mathDoerDivideButton;
public static JButton mathDoerMultiplyButton;
public static JButton mathDoerAdditionButton;
public static JButton mathDoerSubtractionButton;
public static JButton mathDoerNegitiveButton;
public static JButton mathDoerFormulaButton;
public static JButton mathDoerAlphaButton;
public static JButton mathDoerBetaButton;
public static JButton mathDoerGammaButton;
public static JButton mathDoerDeltaButton;
public static JButton mathDoerThirdButton;
public static JButton mathDoerEqualButton;
public static JButton mathDoerStartParentheses;
public static JButton mathDoerEndParentheses;
public static JButton mathDoerCaretKey;
public static JButton mathDoerMainScreen;
public static MathDoerKeyListener sea_MathDoerKeyListener;
public static void main(String[] args) {
//These integers are used to set the size of the frame, and the textPane
int mathDoerWidth = 397;
int mathDoerHeight = 527;
int mathDoerTextWidth = mathDoerHeight - 1 - 71;
//This keyListener just adds keyBoard Controls for each button (I.E. 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, /, *, -, +, Clear, Enter)
sea_MathDoerKeyListener = new MathDoerKeyListener();
//Defining everything to do with the frame, textPane, and scrollPane
mathDoerFrame.setSize(mathDoerWidth, mathDoerHeight);
mathDoerFrame.setLocationRelativeTo(null);
mathDoerFrame.setLayout(null);
mathDoerFrame.setDefaultCloseOperation(2);
mathDoerFrame.setResizable(true);
mathDoerFrame.getContentPane().setBackground(new Color(0, 0, 0));
mathDoerFrame.addKeyListener(sea_MathDoerKeyListener);
mathDoerFrame.setResizable(false);
mathDoerFrame.setVisible(true);
//This text is the History
mathDoerFrame.add(mathDoerText);
mathDoerText.setBackground(new Color(15, 15, 15));
mathDoerText.setForeground(new Color(200, 200, 200));
mathDoerText.setFont(new Font("Courier New", Font.PLAIN, 12));
mathDoerText.setEditable(false);
mathDoerText.addKeyListener(sea_MathDoerKeyListener);
mathDoerTextScoll.setVisible(true);
mathDoerTextScoll = new JScrollPane();
mathDoerTextScoll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
mathDoerTextScoll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
mathDoerTextScoll.setBounds(1, 1, mathDoerTextWidth, 88);
mathDoerTextScoll.getViewport().add(mathDoerText);
mathDoerTextScoll.setBorder(null);
mathDoerFrame.add(mathDoerTextScoll);
//This text is the one the buttons are editing
mathDoerFrame.add(mathDoerConsole);
mathDoerConsole.setBackground(new Color(15, 15, 15));
mathDoerConsole.setForeground(new Color(200, 200, 200));
mathDoerConsole.setFont(new Font("Courier New", Font.PLAIN, 12));
mathDoerConsole.setEditable(true);
mathDoerConsole.addKeyListener(sea_MathDoerKeyListener);
mathDoerConsoleScroll.setVisible(true);
mathDoerConsoleScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
mathDoerConsoleScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
mathDoerConsoleScroll.setBounds(1, 89, mathDoerTextWidth, 18);
mathDoerConsoleScroll.getViewport().add(mathDoerConsole);
mathDoerConsoleScroll.setBorder(null);
mathDoerFrame.add(mathDoerConsoleScroll);
//Used to make a grid system for the buttons
int buttonSize = 75;
int column = buttonSize + 1;
int row = buttonSize + 1;
//Defining all of the Necessary Things for the button (for the Method(I think that's the right term) see line 184)
mathDoerDecimalButton = initButton(column*3+1, row*4+108, buttonSize, buttonSize, ".", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoerNegitiveButton = initButton(column*1+1, row*4+108, buttonSize, buttonSize, "-/+", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoer0Button = initButton(column*2+1, row*4+108, buttonSize, buttonSize, "0", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoer1Button = initButton(column*1+1, row*3+108, buttonSize, buttonSize, "1", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoer2Button = initButton(column*2+1, row*3+108, buttonSize, buttonSize, "2", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoer3Button = initButton(column*3+1, row*3+108, buttonSize, buttonSize, "3", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoer4Button = initButton(column*1+1, row*2+108, buttonSize, buttonSize, "4", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoer5Button = initButton(column*2+1, row*2+108, buttonSize, buttonSize, "5", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoer6Button = initButton(column*3+1, row*2+108, buttonSize, buttonSize, "6", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoer7Button = initButton(column*1+1, row*1+108, buttonSize, buttonSize, "7", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoer8Button = initButton(column*2+1, row*1+108, buttonSize, buttonSize, "8", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoer9Button = initButton(column*3+1, row*1+108, buttonSize, buttonSize, "9", new Color(10, 15, 15), new Color(200, 200, 200), true);
mathDoerDivideButton = initButton(column*4+1, row*0+108, buttonSize, buttonSize, " / ", new Color(15, 10, 15), new Color(200, 200, 200), true);
mathDoerMultiplyButton = initButton(column*4+1, row*1+108, buttonSize, buttonSize, " * ", new Color(15, 10, 15), new Color(200, 200, 200), true);
mathDoerAdditionButton = initButton(column*4+1, row*3+108, buttonSize, buttonSize, " + ", new Color(15, 10, 15), new Color(200, 200, 200), true);
mathDoerSubtractionButton = initButton(column*4+1, row*2+108, buttonSize, buttonSize, " - ", new Color(15, 10, 15), new Color(200, 200, 200), true);
mathDoerAlphaButton = initButton(column*0+1, row*1+108, buttonSize, buttonSize, "α", new Color(15, 15, 10), new Color(200, 200, 200), false);
mathDoerBetaButton = initButton(column*0+1, row*2+108, buttonSize, buttonSize, "β", new Color(15, 15, 10), new Color(200, 200, 200), false);
mathDoerGammaButton = initButton(column*0+1, row*3+108, buttonSize, buttonSize, "γ", new Color(15, 15, 10), new Color(200, 200, 200), false);
mathDoerDeltaButton = initButton(column*0+1, row*4+108, buttonSize, buttonSize, "δ", new Color(15, 15, 10), new Color(200, 200, 200), false);
mathDoerEqualButton = initButton(column*4+1, row*4+108, buttonSize, buttonSize, " = ", new Color(10, 15, 10), new Color(200, 200, 200), false);
mathDoerClearButton = initButton(column*0+1, row*0+108, buttonSize, buttonSize, "Clear", new Color(10, 15, 10), new Color(200, 200, 200), false);
mathDoerStartParentheses = initButton(column*1+1, row*0+108, buttonSize, buttonSize, " ( ", new Color(15, 10, 15), new Color(200, 200, 200), true);
mathDoerEndParentheses = initButton(column*2+1, row*0+108, buttonSize, buttonSize, " ) ", new Color(15, 10, 15), new Color(200, 200, 200), true);
mathDoerCaretKey = initButton(column*3+1, row*0+108, buttonSize, buttonSize, " ^ ", new Color(15, 10, 15), new Color(200, 200, 200), true);
//These the specialized buttons when pressed
mathDoerAlphaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Formula Screen");
}
});
mathDoerEqualButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculate();
}
});
mathDoerClearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mathDoerConsole.setText("");
System.out.println("clear");
}
});
}
public static JButton initButton(int x, int y, int width, int height, String text, Color backgroundColor, Color foregroundColor, boolean defaultAction) {
JButton button = new JButton(text);
mathDoerFrame.add(button);
button.setBounds(x, y , width, height);
button.setBackground(backgroundColor);
button.setForeground(foregroundColor);
button.setBorderPainted(false);
button.addKeyListener(sea_MathDoerKeyListener);
if (defaultAction) {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(button.getText());
mathDoerConsole.setText(mathDoerConsole.getText() + button.getText());
}
});
}
return button;
}
//The method is the main calculation
public static void calculate() {
System.out.println(mathDoerConsole.getText());
double math = Double.parseDouble(mathDoerConsole.getText());
System.out.println(Double.toString(math));
}
}
If you need anymore information of anything please feel free to let me know, or ask