GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Apr-2008, 17:37
msnider9 msnider9 is offline
New Member
 
Join Date: Apr 2008
Posts: 1
msnider9 is on a distinguished road
Unhappy

Errors in Compiling


I am getting very confused. Here is my java program

JAVA Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
public class Calculator2 extends JFrame implements ActionListener {
private JPanel panelAdder;
private JLabel labela;
private JLabel labelt;
private JLabel labelr;
private JTextField textFieldAmount;
private JTextField textFieldTerm;
private JTextField textFieldRate;
private JTextField textFieldResult;
private JTextField textFieldClear;
private JButton buttonCalc;
private JButton buttonClear;
 
//Clear Fields
private boolean clearText=true;
String selection=("");
 
 
public Calculator2() {
super("Calculator");
initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
 
// Add Listeners
buttonCalc.addActionListener(this);
buttonClear.addActionListener(this);
 
}
public void initComponents() {
//Initialize Components
panelAdder = new JPanel();
labela = new JLabel("Amount");
textFieldAmount = new JTextField();
labelt = new JLabel("Term");
textFieldTerm = new JTextField();
labelr = new JLabel("Rate");
textFieldRate = new JTextField();
textFieldResult = new JTextField();
JButton calculate = new JButton("Calculate");
JTextFieldClear = new JTextField("");
JButton clear = new JButton("Clear");
JButton exit = new JButton("Exit");
}
 
public void actionPerformed(ActionEvent event) {
Object source = event.getSource ();
 
if (source == buttonCalc); {
setResultValue();
else if (event.getSource() == JButton("Clear")) {
clearText=true; //set clear text flag to true for new calculation
JTextFieldAmount.setText(""); //set text field to null string (clear)
JTextFieldTerm.setText(""); 
JTextFieldRate.setText(""); 
JTextFieldAmount.requestFocus(); //return focus to Amount 
} 
//Set Object Attributes
textFieldResult.setEditable(false);
textFieldResult.setColumns(8);
textFieldAmount.setColumns(6);
textFieldTerm.setColumns(2);
textFieldRate.setColumns(2);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
}
// add components to the panel
public void setResultValue() {
double amount = Double.parseDouble (textFieldAmount.getText());
double rate = Double.parseDouble (textFieldRate.getText());
double term = Integer.parseInt(textFieldTerm.getText());
double result = amount * ( rate * Math.pow ( ( 1 + rate ), term ) ) / ( Math.pow( ( 1 + rate ), term ) - 1 );
 
textFieldResult.setText(Double.toString(result));
}
} 
public class Calculator {
public static void main(String[] arguments) {
Calculator frame = new Calculator();
}
}
}


The errors are shown as:

line 57 else without if
else if (event.getSource() == JButton("Clear")) {

line 74 illegal start of expression & ; expected
I did not think you needed a ;

Please help.
Last edited by admin II : 27-Apr-2008 at 18:06. Reason: Changed [QUOTE] to [JAVA]
  #2  
Old 28-Apr-2008, 07:54
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 511
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Errors in Compiling


JAVA Code:
/*if (source == buttonCalc); {
setResultValue();
else if (event.getSource() == JButton("Clear")) {
clearText=true; //set clear text flag to true for new calculation*/
if (source == buttonCalc)
{
  setResultValue();
}
else if (event.getSource() == JButton("Clear")) 
{
  clearText=true; //set clear text flag to true for new calculation
}
  #3  
Old 28-Apr-2008, 08:54
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 966
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Errors in Compiling


There are MANY other issues than just what is needed to clear those first couple of compile errors...

1. Note here:
JAVA Code:
if (source == buttonCalc); {   // no semicolon needed after the condition
2. The 'else without if', comes about from misplacement. See that the first if (with buttonCalc ) starts a new scope with the opening {, but inside that scope, an 'else' is encountered that has no matching 'if'. That "inner else" is NOT a match to the outer if, that is the complaint. (Do you follow?)

3. "Illegal start of expression", comes from the point where a function is being defined INSIDE of another function. You are defining setResultValue(), inside the body of function actionPerformed().

There will be more errors, but clear these up first, and post again if help is still needed. It appears that this Calculator2 class is a copy-paste clone of class Calculator? (Calculator is defined as an inner class [where the main() is defined], so I'm not sure what the purpose for it is??)
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling errors in binary tree code rahul_2550 C Programming Language 1 22-Mar-2007 15:07
errors when compiling 'line example' from help > index abakws C++ Forum 9 09-Nov-2006 03:40
linker errors while compiling sqlite3 with a program ryver C Programming Language 5 17-Aug-2006 00:38
In Need of Some Serious Help...compiling errors [C++] needcishelp C++ Forum 25 21-Dec-2005 01:26
Compiling Errors ToddSAFM C++ Forum 22 18-Dec-2004 11:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:12.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.