Seven Errors, Need Help with Algebra-Solving Application

Page 1 of 1

14 Replies - 1914 Views - Last Post: 28 June 2009 - 05:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=111895&amp;s=9db54c5515872065b920606bed8b8a2a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 wolfman29

Reputation: 1
• Posts: 68
• Joined: 24-June 09

Seven Errors, Need Help with Algebra-Solving Application

Posted 25 June 2009 - 11:29 PM

Hey everyone.

I am in the final stages of developing my NON-HOMEWORK algebra-solving application.

Here is the source code:

```import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class singleVariableSolver extends JPanel implements ActionListener {

public static void main (String[] args) {

//Setting Variables

String equation;
Float coefficient;
String afterVariable1;
int operatorCheck;
String afterVariable2;
String afterVariable3;
Float equals;
Float constant = null;
Float divisor;
Float solution;
String textSolution;

//Beginning of GUI portion

//Creating, showing, and laying out of frame

JFrame singleVariableFrame = new JFrame("Single Variable Equation Solver");
singleVariableFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
singleVariableFrame.setVisible(true);
singleVariableFrame.setLayout(null);
singleVariableFrame.setResizable(false);

//Content

JLabel equationLabel = new JLabel("Equation: ");
JLabel solutionLabel = new JLabel("Solution: ");

JTextField equationTextfield = new JTextField(15);

//Instructions

JLabel instructions1 = new JLabel("Please insert your equation that you want solved of");
JLabel instructions2 = new JLabel("the format '4x+2=10'. Please use 'x' for your variable.");

//Solve Button

JButton solveButton = new JButton("Solve!");
solveButton.setPreferredSize(new Dimension(100,30));

JTextArea solutionTextArea = new JTextArea("", 1, 15);
solutionTextArea.setEditable(false);

//Absolute Positioning

Insets insets = singleVariableFrame.getInsets();
Dimension size = instructions1.getPreferredSize();

size = instructions1.getPreferredSize();
instructions1.setBounds(130 + insets.left, 75 + insets.top, size.width, size.height);

size = instructions2.getPreferredSize();
instructions2.setBounds(128 + insets.left, 90 + insets.top, size.width, size.height);

size = equationLabel.getPreferredSize();
equationLabel.setBounds(150 + insets.left, 150 + insets.top, size.width, size.height);

size = equationTextfield.getPreferredSize();
equationTextfield.setBounds(210 + insets.left, 150 + insets.top, size.width, size.height);

size = solveButton.getPreferredSize();
solveButton.setBounds(240 + insets.left, 195 + insets.top, size.width, size.height);

size = solutionLabel.getPreferredSize();
solutionLabel.setBounds(150 + insets.left, 250 + insets.top, size.width, size.height);

size = solutionTextArea.getPreferredSize();
solutionTextArea.setBounds(213 + insets.left, 250 + insets.top, size.width, size.height);

//Setting size of frame

singleVariableFrame.setSize(600 + insets.left + insets.right,
400 + insets.top + insets.bottom);
singleVariableFrame.setVisible(true);

//Setting listener to button
solveButton.setActionCommand("solve");

//Setting button tip
solveButton.setToolTipText("Click this button to solve the equation.");

equation = "";

String[] brokenUp = equation.split("x");

coefficient = Float.parseFloat(brokenUp[0]);

afterVariable1 = brokenUp[1];

operatorCheck = equation.indexOf("+");

if (operatorCheck > 0) {

String[] brokenUp2 = afterVariable1.split("\\+");
afterVariable2 = brokenUp2[1];
String[] brokenUp3 = afterVariable2.split("=");

constant = Float.parseFloat(brokenUp3[0]);

}

else {}

operatorCheck = equation.indexOf("-");

if (operatorCheck > 0) {

String[] brokenUp2 = afterVariable1.split("-");
afterVariable2 = brokenUp2[1];
String[] brokenUp3 = afterVariable2.split("=");

constant = Float.parseFloat(brokenUp3[0]);

}

else {}

String[] brokenUp4 = equation.split("=");
equals = Float.parseFloat(brokenUp4[1]);

divisor = equals-constant;

solution = divisor/coefficient;

JOptionPane.showMessageDialog(null, "x = " + solution);

}

public void actionPerformed(ActionEvent e) {

if ("solve".equals(e.getActionCommand())) {

equation = equationTextfield.getText();
textSolution = String.valueOf(solution);
solutionTextArea.append(textSolution);

}

}

}
```

I think I have everything worked out, and if you have any questions as to what each thing does, just ask. But here are the errors I am getting:

singleVariableSolver.java:109: non-static variable this cannot be referenced fro
m a static context
^
singleVariableSolver.java:171: cannot find symbol
symbol : variable equation
location: class singleVariableSolver
equation = equationTextfield.getText();
^
singleVariableSolver.java:171: cannot find symbol
symbol : variable equationTextfield
location: class singleVariableSolver
equation = equationTextfield.getText();
^
singleVariableSolver.java:172: cannot find symbol
symbol : variable textSolution
location: class singleVariableSolver
textSolution = String.valueOf(solution);
^
singleVariableSolver.java:172: cannot find symbol
symbol : variable solution
location: class singleVariableSolver
textSolution = String.valueOf(solution);
^
singleVariableSolver.java:173: cannot find symbol
symbol : variable textSolution
location: class singleVariableSolver
solutionTextArea.append(textSolution);
^
singleVariableSolver.java:173: cannot find symbol
symbol : variable solutionTextArea
location: class singleVariableSolver
solutionTextArea.append(textSolution);
^
6 errors

I know why these last six errors are happening - they are happening because I need to reference exactly what I variables/fields I am using in this ActionPerformed method. But I don't know how to fix it. But the first error... I have no idea what to do.

Anyone have any ideas?

THIS IS NOT A HOMEWORK ASSIGNMENT!!!

This post has been edited by wolfman29: 25 June 2009 - 11:30 PM

Is This A Good Question/Topic? 0

Replies To: Seven Errors, Need Help with Algebra-Solving Application

#2 nick2price

• D.I.C Lover

Reputation: 565
• Posts: 2,826
• Joined: 23-November 07

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 12:15 AM

First thing to do is take all that code out of your main method and put it into a non static method or even the constructor.

#3 wolfman29

Reputation: 1
• Posts: 68
• Joined: 24-June 09

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 01:00 AM

nick2price, on 25 Jun, 2009 - 11:15 PM, said:

First thing to do is take all that code out of your main method and put it into a non static method or even the constructor.

I tried that. But then I got even more disgusting errors which I didn't know how to fix.

#4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 01:42 PM

wolfman29, on 26 Jun, 2009 - 12:00 AM, said:

nick2price, on 25 Jun, 2009 - 11:15 PM, said:

First thing to do is take all that code out of your main method and put it into a non static method or even the constructor.

I tried that. But then I got even more disgusting errors which I didn't know how to fix.

Do what Nick said to you

this does not exist.. this should be an instance of singleVariableSolver

You need to create a singleVariableObject() and perform your operations into it

JFrame singleVariableFrame = new JFrame("Single Variable Equation Solver");
singleVariableFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
singleVariableFrame.setVisible(true);
singleVariableFrame.setLayout(null);
singleVariableFrame.setResizable(false);

the first line is OK it can be executed in the instance variables area
but the 4 following lines are code... they have to be executed inside a metod() or constructor

#5 wolfman29

Reputation: 1
• Posts: 68
• Joined: 24-June 09

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 07:10 PM

Okay, so I did some more research and I edited my code quite a bit. Currently, with this code, it SHOULD just returns the equation entered into the equation box into the solution box - the math I can add in later once I get it compiling correctly. I only receive one error now... here is the code:

```package components;

import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;

public class SingleVariableCalc extends JPanel implements ActionListener {

protected JTextField equationTextfield = new JTextField(15);

protected JLabel equationLabel = newJLabel("Equation: ");
protected JLabel solutionLabel = new JLabel("Equation: ");

JLabel instructions1 = new JLabel("Please insert your equation that you want solved of");
JLabel instructions2 = new JLabel("the format '4x+2=10'. Please use 'x' for your variable.");

JButton solveButton = new JButton("Solve!");

JTextField solutionTextField = new JTextField(15);

public ComponentModifier() {

super(null);

solutionTextField.setEditable(false);

Dimension size = instructions1.getPreferredSize();

size = instructions1.getPreferredSize();
instructions1.setBounds(130, 75, size.width, size.height);

size = instructions2.getPreferredSize();
instructions2.setBounds(128, 90, size.width, size.height);

size = equationLabel.getPreferredSize();
equationLabel.setBounds(150, 150, size.width, size.height);

size = equationTextfield.getPreferredSize();
equationTextfield.setBounds(210, 150, size.width, size.height);

size = solveButton.getPreferredSize();
solveButton.setBounds(240, 195, size.width, size.height);

size = solutionLabel.getPreferredSize();
solutionLabel.setBounds(150, 250, size.width, size.height);

size = solutionTextField.getPreferredSize();
solutionTextField.setBounds(213, 250, size.width, size.height);
}

public void actionPerformed(ActionEvent evt) {

//Setting Variables

String equation = equationTextfield.getText();

Float coefficient;
String afterVariable1;
int operatorCheck;
String afterVariable2;
String afterVariable3;
Float equals;
Float constant = null;
Float divisor;
Float solution;
String textSolution;

text.setText(equation);

}

private static void createAndShowGUI() {

//Create and set up the window.
JFrame singleVariableCalculator= new JFrame("Single Variable Equation Solver");
singleVariableCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Display the window.
singleVariableCalculator.pack();
singleVariableCalculator.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event dispatch thread:
//Creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
```

Here is the error I am getting:
singleVariableCalc.java:27: invalid method declaration; return type required
public ComponentModifier() {
^

I don't know what type of variable I should return, since really I am just returning all those components to be added to the GUI.

Any ideas?

And if you could post some edited code on here, that would be much appreciated.

#6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 07:50 PM

so it is

public void componentModifier()

but I am wondering what the

super(null) the first line in it mens or tries to mean

SingleVariableCals extends JPanel so

super(null) calls the constructor of JPanel with null as argument but you don't save that JPanel nowhere
actually your super(null) is equavalent to

new JPanel(null);

as you don't save this JPanel nowhere (not JPanel abc = super(null) this stement is useless and must be a relic of your old code

#7 wolfman29

Reputation: 1
• Posts: 68
• Joined: 24-June 09

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 08:58 PM

pbl, on 26 Jun, 2009 - 06:50 PM, said:

so it is

public void componentModifier()

but I am wondering what the

super(null) the first line in it mens or tries to mean

SingleVariableCals extends JPanel so

super(null) calls the constructor of JPanel with null as argument but you don't save that JPanel nowhere
actually your super(null) is equavalent to

new JPanel(null);

as you don't save this JPanel nowhere (not JPanel abc = super(null) this stement is useless and must be a relic of your old code

Well, actually, here is the code I based this code off of:
```import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextDemo extends JPanel implements ActionListener {
protected JTextField textField;
protected JTextArea textArea;
private final static String newline = "\n";

public TextDemo() {
super(new GridBagLayout());

textField = new JTextField(20);

textArea = new JTextArea(5, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);

GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;

c.fill = GridBagConstraints.HORIZONTAL;

c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
}

public void actionPerformed(ActionEvent evt) {
String text = textField.getText();
textArea.append(text + newline);
textField.selectAll();

//Make sure the new text is visible, even if there
//was a selection in the text area.
textArea.setCaretPosition(textArea.getDocument().getLength());
}

/**
* Create the GUI and show it.  For thread safety,
* this method should be invoked from the
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
```

This code uses super(new GridBagLayout()); to define what the layout is of the frame (I think). So, because I am defining the locations of all my components using absolute location, I decided to give it a layout of null, which I thought would work.

Anyway, do you have a solution to the error I posted?

This post has been edited by wolfman29: 26 June 2009 - 08:59 PM

#8 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 09:17 PM

wolfman29, on 26 Jun, 2009 - 07:58 PM, said:

Anyway, do you have a solution to the error I posted?

You error is:

method(variable); or may be method(type);

the good syntax is

method(type variable);

#9 wolfman29

Reputation: 1
• Posts: 68
• Joined: 24-June 09

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 09:23 PM

pbl, on 26 Jun, 2009 - 08:17 PM, said:

wolfman29, on 26 Jun, 2009 - 07:58 PM, said:

Anyway, do you have a solution to the error I posted?

You error is:

method(variable); or may be method(type);

the good syntax is

method(type variable);

I am confused.

You mean my componentModifier? Or super()? I am not sure which you are talking about. And what would I put for, then, to correct the error?

#10 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 09:41 PM

Sorry you made a lot of changes in your code between the 2 questions
the only thing missing is may be is a
textField.setText(null);
after you appended the
String text = textField.getText();
into the textArea

#11 wolfman29

Reputation: 1
• Posts: 68
• Joined: 24-June 09

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 09:45 PM

pbl, on 26 Jun, 2009 - 08:41 PM, said:

Sorry you made a lot of changes in your code between the 2 questions
the only thing missing is may be is a
textField.setText(null);
after you appended the
String text = textField.getText();
into the textArea

Sorry, you misunderstood me. The LAST code posted was not the code for my app. It's the code I based my app off of. So I was using that to show you what the "super()" method was doing. MY code is the code before that one

THAT one is the one that doesn't work.

#12 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 09:50 PM

works for me

#13 wolfman29

Reputation: 1
• Posts: 68
• Joined: 24-June 09

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 26 June 2009 - 09:55 PM

``` package components;

import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;

public class SingleVariableCalc extends JPanel implements ActionListener {

protected JTextField equationTextfield = new JTextField(15);

protected JLabel equationLabel = newJLabel("Equation: ");
protected JLabel solutionLabel = new JLabel("Equation: ");

JLabel instructions1 = new JLabel("Please insert your equation that you want solved of");
JLabel instructions2 = new JLabel("the format '4x+2=10'. Please use 'x' for your variable.");

JButton solveButton = new JButton("Solve!");

JTextField solutionTextField = new JTextField(15);

public ComponentModifier() {

super(null);

solutionTextField.setEditable(false);

Dimension size = instructions1.getPreferredSize();

size = instructions1.getPreferredSize();
instructions1.setBounds(130, 75, size.width, size.height);

size = instructions2.getPreferredSize();
instructions2.setBounds(128, 90, size.width, size.height);

size = equationLabel.getPreferredSize();
equationLabel.setBounds(150, 150, size.width, size.height);

size = equationTextfield.getPreferredSize();
equationTextfield.setBounds(210, 150, size.width, size.height);

size = solveButton.getPreferredSize();
solveButton.setBounds(240, 195, size.width, size.height);

size = solutionLabel.getPreferredSize();
solutionLabel.setBounds(150, 250, size.width, size.height);

size = solutionTextField.getPreferredSize();
solutionTextField.setBounds(213, 250, size.width, size.height);
}

public void actionPerformed(ActionEvent evt) {

//Setting Variables

String equation = equationTextfield.getText();

Float coefficient;
String afterVariable1;
int operatorCheck;
String afterVariable2;
String afterVariable3;
Float equals;
Float constant = null;
Float divisor;
Float solution;
String textSolution;

text.setText(equation);

}

private static void createAndShowGUI() {

//Create and set up the window.
JFrame singleVariableCalculator= new JFrame("Single Variable Equation Solver");
singleVariableCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Display the window.
singleVariableCalculator.pack();
singleVariableCalculator.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event dispatch thread:
//Creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

```

Here is MY code.

#14 nick2price

• D.I.C Lover

Reputation: 565
• Posts: 2,826
• Joined: 23-November 07

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 27 June 2009 - 02:25 AM

There is a lot of stuff you can do better in this, but I have changed a coupld of things so that your gui shows up. You just need to sort out the mathematics of it
```import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;

public class SingleVariableCalc extends JPanel implements ActionListener {

protected JTextField equationTextfield = new JTextField(15);

protected JLabel equationLabel = new JLabel("Equation: ");
protected JLabel solutionLabel = new JLabel("Equation: ");

JLabel instructions1 = new JLabel("Please insert your equation that you want solved of");
JLabel instructions2 = new JLabel("the format '4x+2=10'. Please use 'x' for your variable.");

JButton solveButton = new JButton("Solve!");

JTextField solutionTextField = new JTextField(15);

public SingleVariableCalc() {

super(null);

solutionTextField.setEditable(false);

Dimension size = instructions1.getPreferredSize();

size = instructions1.getPreferredSize();
instructions1.setBounds(130, 75, size.width, size.height);

size = instructions2.getPreferredSize();
instructions2.setBounds(128, 90, size.width, size.height);

size = equationLabel.getPreferredSize();
equationLabel.setBounds(150, 150, size.width, size.height);

size = equationTextfield.getPreferredSize();
equationTextfield.setBounds(210, 150, size.width, size.height);

size = solveButton.getPreferredSize();
solveButton.setBounds(240, 195, size.width, size.height);

size = solutionLabel.getPreferredSize();
solutionLabel.setBounds(150, 250, size.width, size.height);

size = solutionTextField.getPreferredSize();
solutionTextField.setBounds(213, 250, size.width, size.height);
}

public void actionPerformed(ActionEvent evt) {

//Setting Variables

String equation = equationTextfield.getText();

Float coefficient;
String afterVariable1;
int operatorCheck;
String afterVariable2;
String afterVariable3;
Float equals;
Float constant = null;
Float divisor;
Float solution;
String textSolution;

solutionTextField.setText(equation);

}

private static void createAndShowGUI() {

//Create and set up the window.
JFrame singleVariableCalculator= new JFrame("Single Variable Equation Solver");
singleVariableCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Display the window.
singleVariableCalculator.pack();
singleVariableCalculator.setSize(800,600);
singleVariableCalculator.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event dispatch thread:
//Creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

```

#15 wolfman29

Reputation: 1
• Posts: 68
• Joined: 24-June 09

Re: Seven Errors, Need Help with Algebra-Solving Application

Posted 28 June 2009 - 05:18 PM

Thanks a bunch! It works perfectly! But if you would, mind explaining here or over email what you did and why that error was there in the beginning?