I am very green at writing Java, I am trying to go through our chapter examples and putting in data for a mortgage calculator with a GUI. This code is far from finished but the example says
"Now save and compile" and this is where I am getting the error "Java 42 illegal start of expression".

I would appreciate any suggestions or explanation of what that error is REALLY telling me!:(

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MortgageCalculator extends JApplet
implements ActionListener

{
	Label greeting = new Label("Mortgage Calculator");
	Font Big = new font("Times Roman",Font.ITALIC, 24);

	 public void init();
	 public void actionPerformed(ActionEvent thisEvent)
	 {
		 Object source = thisEvent.getSource();
		 if(source==principal)
		 {
			 String name = answer.getText();
			 personalGreeting.setText("Thank you"+ name);

		 }
	 }
	 {
		 add(greeting);
		 greeting.setFont(bigFont);

		 JButton principal = new JButton ("Principal");
		 principal.addActionListener(this);
		 answer.addActionListener(this);
		 JTextField answer = new JTextField("",10);
		 JLabel personalGreeting = newJLabel("");
		 FlowLayout flow = new FlowLayout();

		  if (source instanceof JTextField);
		 {
			 String name = answer.getText();
			 personalGreeting.setText("Thank you" + name);
		 }

		{
	JLabel greeting = new JLabel("Principal");
	public void init();
		{
			Container con = getContentPane();
			con.add(greeting);
			con.add(answer);
			con.add(principal);
			con.add(personalGreeting);
			answer.requestFocus();
		}
	}

 }
}

No semicolon between public void init() and {

No semicolon between public void init() and {

I added one and now I am getting same error plus "not a statement error. Thank you so much foir helping me!

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MortgageCalculator extends JApplet
implements ActionListener

{
	Label greeting = new Label("Mortgage Calculator");
	Font Big = new font("Times Roman",Font.ITALIC, 24);

	 public void init();
	 public void actionPerformed(ActionEvent thisEvent)
	 {
		 Object source = thisEvent.getSource();
		 if(source==principal)
		 {
			 String name = answer.getText();
			 personalGreeting.setText("Thank you"+ name);

		 }
	 }
	 {
		 add(greeting);
		 greeting.setFont(bigFont);

		 JButton principal = new JButton ("Principal");
		 principal.addActionListener(this);
		 answer.addActionListener(this);
		 JTextField answer = new JTextField("",10);
		 JLabel personalGreeting = newJLabel("");
		 FlowLayout flow = new FlowLayout();

		  if (source instanceof JTextField);
		 {
			 String name = answer.getText();
			 personalGreeting.setText("Thank you" + name);
		 }

		{
	JLabel greeting = new JLabel("Principal");
	public void init;();
		{
			Container con = getContentPane();
			con.add(greeting);
			con.add(answer);
			con.add(principal);
			con.add(personalGreeting);
			answer.requestFocus();
		}
	}

 }
}

sry but jamesCherril was wrong, you didnt need that extra semi-colon, i cant solve your over problem though, im not sure what public void init() is or does

unless he means that there shouldnt be a semi-colon after public void init() which means it would appear like:

public void init()
		{
			Container con = getContentPane();
			con.add(greeting);
			con.add(answer);
			con.add(principal);
			con.add(personalGreeting);
			answer.requestFocus();
		}

Now I am down to just 2 errors both are illegal start of expression.
1 on public and 1 on void!

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MortgageCalculator extends JApplet
implements ActionListener

{
	Label greeting = new Label("Mortgage Calculator");
	Font Big = new font("Times Roman",Font.ITALIC, 24);

	 public void init();
	 public void actionPerformed(ActionEvent thisEvent)
	 {
		 Object source = thisEvent.getSource();
		 if(source==principal)
		 {
			 String name = answer.getText();
			 personalGreeting.setText("Thank you"+ name);

		 }
	 }
	 {
		 add(greeting);
		 greeting.setFont(bigFont);

		 JButton principal = new JButton ("Principal");
		 principal.addActionListener(this);
		 answer.addActionListener(this);
		 JTextField answer = new JTextField("",10);
		 JLabel personalGreeting = newJLabel("");
		 FlowLayout flow = new FlowLayout();

		  if (source instanceof JTextField);
		 {
			 String name = answer.getText();
			 personalGreeting.setText("Thank you" + name);
		 }

		{
	JLabel greeting = new JLabel("Principal");
	public void init;
		{
			Container con = getContentPane();
			con.add(greeting);
			con.add(answer);
			con.add(principal);
			con.add(personalGreeting);
			answer.requestFocus();
		}
	}

 }
}

Thanx that is just what I tried only with a semi colan, maybe I will try it w/o!

youve taken the brackets of the method

i posted this before i saw your second message after my first

Nope!! Gotta have the semi colon. Back to trial and error!! Thanks for your help!

I have to have a application for a mortgage calculator that had a GUI and allwed user input. I thought I could do it by following examples in the book because I am clueless. Might have to do something different!!

No semicolon between public void init() and {

When I wrote that I expected that you would realise I was saying there should be no ; between the () and the {
ie it should read

public void init() { ...

Seemed clear enough to me...

sry but jamesCherril was wrong, you didnt need that extra semi-colon, i cant solve your over problem though, im not sure what public void init() is or does...

Sorry man, you really should read the post properly before telling someone they are wrong in public (even more so if you don't know the syntax of a method declaqration). I didn't suggest another ;, especially not where the OP then put it. If you read what I wrote carefully and match it to the code he posted there's only one way to interpret it.
ps That's James with a capital "J" and Cherrill with two "l"s

Sorry James,
Like I said I am not getting this very good. I have trouble knowing what the statements, arrays, components, etc, etc mean. I really appreciate your help however. Thank you very much!

Sorry James,
Like I said I am not getting this very good. I have trouble knowing what the statements, arrays, components, etc, etc mean. I really appreciate your help however. Thank you very much!

Don't worry! We all started from the same point. Stick with it and it will get easier. You soon get to know what the most common messages mean - in this case "illegal start of expression" usually means that there's an error immediately before where it says, ie it's saying "I didn't expect you to start another statement just yet". In your case, that was because you had finished the previous code (by adding a semicolon) too early.
ps Sorry if I referred to you as "he" when if it should have been "she". It's hard to know who you're dealing with behind an internet ID :-)

No worries! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.