I am trying to compile my simple java code and receive two errors.

Any help will be appreciated.

1. Java1_Week2.java:19: illegal start of expression
public Java1_Week2() {

2. Java1_Week2.java:19: ';' expected
public Java1_Week2(){

Here is my code:

import java.text.DecimalFormat;

import javax.swing.*;

public class Java1_Week2 {

    public static void main(String[] args) {


JFrame mainWindow = new JFrame();

public Java1_Week2() {

// Declare method variables
double mortTerm = 30;
double mortRate = .0575;
double mortAmount = 200000;
double MT, MA, MR;

// Format the decimal place of the final number
DecimalFormat forma = new DecimalFormat("#,###0.00");

//Declare variables with amounts
MT = mortTerm*12;
MR = mortRate/12;
MA = mortAmount;


//Calculation of mortgate payment
double payment = (MA*(MR))/(1-(Math.pow(1/(1+(MR)),(MT))));

//Display amount of monthly payment
System.out.println("The monthly loan amount for a $200,000"
 + " loan at %5.75 interest rate is $" + forma.format(payment) + ".");

mainWindow.setTitle("Mortgage Calculator");


6 Years
Discussion Span
Last Post by Spirit_Guardian

For one you are trying to declare something inside the main method. That doesn't work. For a second thing, what is it? if it is a class, add that keyword, and put it in another java file, of course, if it is a method, then declare the return type.


Looks like it's meant to be a constructor, isn't it? Whatever it is, if you take out that line and one of your close curlies, you might have a legal program that does some calculation and then displays a JFrame with nothing in it.
At least, the rest of it looks like legal Java.


Thank you very much, that was the problem. I removed the offending line and it worked perfectly.

I was going through a video showing how to create a gui interface for a java application and wanted to try creating an empty form first. Thanks again for the help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.