2

Ok, I'm kinda new to java. I'm making a program that solves for one step equations. I'm having some difficulties running it though. Here is the code for my main file, Main.java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    Solve solve = new Solve();
    JButton add = new JButton("Add");
    JButton sub = new JButton("Subtract");
    JButton mult = new JButton("Multiply");
    JButton div = new JButton("Divide");
    JButton solv = new JButton("Solve!");
    JTextArea one = new JTextArea();
    JLabel two = new JLabel(" = ");
    JLabel three = new JLabel("X");
    JLabel four = new JLabel();
    JTextArea five = new JTextArea();
    JLabel solved = new JLabel();
    JPanel row1 = new JPanel();
    JPanel row2 = new JPanel();
    JPanel row3 = new JPanel();

    public double funct;

    public Main() {
        super("Solving a one step equation!");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        GridLayout layout = new GridLayout();
        setLayout(layout);

        FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER);

        row1.setLayout(layout1);
        row1.add(add);
        row1.add(sub);
        row1.add(mult);
        row1.add(div);
        row1.add(solv);
        add(row1);
        add.addActionListener(this);
        sub.addActionListener(this);
        mult.addActionListener(this);
        div.addActionListener(this);
        solv.addActionListener(this);

        GridLayout layout2 = new GridLayout(1, 1, 1, 1);
        row2.setLayout(layout2);
        row2.add(one, BorderLayout.CENTER);
        row2.add(two, BorderLayout.CENTER);
        row2.add(three, BorderLayout.CENTER);
        row2.add(four, BorderLayout.CENTER);
        row2.add(five);
        add(row2, BorderLayout.CENTER);

        GridLayout layout3 = new GridLayout(5, 5, 5, 5);
        row3.setLayout(layout3);
        row3.add(solved);
        add(row3);
    }

    public static void main(String[] args) {
        Main frame = new Main();
    }

    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if(source == add)
        {
            four.setText(" + ");
            funct = 1;
        }
        else if(source == sub)
        {
            four.setText(" - ");
            funct = 2;
        }
        else if(source == mult)
        {
            four.setText(" * ");
            funct = 3;
        }
        else if(source == div)
        {
            four.setText(" / ");
            funct = 4;
        }
        if(source == solv)
        {
            if(funct == 1)
            {

                double Ones = Double.parseDouble(three.getText());
                double Twos = Double.parseDouble(three.getText());
                solved.setText("X = " + solve.Add(Ones, Twos));
            }
            else if(funct == 2)
            {
                double Ones = Double.parseDouble(three.getText());
                double Twos = Double.parseDouble(three.getText());
                solved.setText("X = " + solve.Sub(Ones, Twos));
            }
            else if(funct == 3)
            {
                double Ones = Double.parseDouble(three.getText());
                double Twos = Double.parseDouble(three.getText());
                solved.setText("X = " + solve.Mult(Ones, Twos));
            }
            else if(funct == 4)
            {
                double Ones = Double.parseDouble(three.getText());
                double Twos = Double.parseDouble(three.getText());
                solved.setText("X = " + solve.Div(Ones, Twos));
            }

        }
    }

}

Here is the code for my other file, Solve.java

public class Solve {
    public double Add(double One, double Two)
    {
        return One - Two;
    }

    public double Sub(double One, double Two)
    {
        return One + Two;
    }

    public double Mult(double One, double Two)
    {
        return One / Two;
    }

    public double Div(double One, double Two)
    {
        return One * Two;
    }
}

Some help would be appreciated. Anyone see what I'm doing wrong?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
The_Steve13
  • 119
  • 2
  • 11
  • 1
    You might consider telling us more of the details about your problem. – Hovercraft Full Of Eels Jun 16 '12 at 18:12
  • 3
    Yup. What you're doing wrong is: 1. Posting too much code. Nobody has time to analyze all your code and search for bugs. 2. Not asking proper questions. 3. Not telling us, what the actual problem is. – npe Jun 16 '12 at 18:13

2 Answers2

5

You get a NumberFormatException once 'Solve' button is clicked. It seems like a copy/paste issue - you are not retrieving the correct numbers. You are trying to convert 'X' string to double. It is best if you give meaningful names to your variables. To fix the exception, try this, replace :

double Ones = Double.parseDouble(three.getText());
double Twos = Double.parseDouble(three.getText());

with:

double Ones = Double.parseDouble(one.getText());
double Twos = Double.parseDouble(five.getText());

Get familiar with Java Code Conventions, Naming Conventions section in particular.

tenorsax
  • 21,123
  • 9
  • 60
  • 107
4

In addition to @Max's helpful answer, here are a few other suggestions:

  • Setting the frame's layout to new GridLayout() defaults to a single row and column with no padding. As an alternative, consider new GridLayout(0, 1, 5, 5), which produces any number of rows in one column with 5x5 padding. Then you can focus on the layout of each row:

    row1.setLayout(new FlowLayout(FlowLayout.CENTER));
    row2.setLayout(new FlowLayout(FlowLayout.CENTER));
    row3.setLayout(new GridLayout(1, 1, 5, 5));
    
  • Move your setVisible() call to the end of the frame's constructor:

    pack();
    setLocationRelativeTo(null);
    setVisible(true);
    
  • Consider getRootPane().setDefaultButton(solv) to make the Solve button the default.

  • Consider making addition the default:

    private JLabel four = new JLabel("+");
    private int funct = 1; // add by default
    
  • Consider using JTextField for number entry:

    private JTextField one = new JTextField(10);
    private JTextField five = new JTextField(10);
    
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • +1 first time to know that `FlowLayout` has `FlowLayout.CENTER`. – Eng.Fouad Jun 16 '12 at 22:50
  • @Eng.Fouad: Good point; it's the default `align` for [`FlowLayout`](http://docs.oracle.com/javase/6/docs/api/java/awt/FlowLayout.html#FlowLayout%28%29). As you know, `FlowLayout` is the default layout for `JPanel`, but it's sometimes useful to make it explicit. – trashgod Jun 17 '12 at 02:46