wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Problems with this weeks code..Still not understanding Illegal start of expression...HELP PLEASE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems with this weeks code..Still not understanding Illegal start of expression...HELP PLEASE" Watch "Problems with this weeks code..Still not understanding Illegal start of expression...HELP PLEASE" New topic
Author

Problems with this weeks code..Still not understanding Illegal start of expression...HELP PLEASE

LeeAnne Murphy
Greenhorn

Joined: Aug 15, 2011
Posts: 18

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

So where are you getting the error? Can you post the stack trace?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
LeeAnne Murphy
Greenhorn

Joined: Aug 15, 2011
Posts: 18

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression
at mortcalcjp2week3.Mortcalcjp2week3.main(Mortcalcjp2week3.java:58)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

I know it is in line 58 which is this part but I am so lost.


/**
* @param args the command line arguments
*/
public static void main(String[] args) {

// Requestor - Ninfa Pendleton - Rapid City, SD
// PRG/421
// August 22, 2011

// Service Request SR-mf-003 Mortgage Payment Calculator. Change Request #4:Write the program in Java (with a graphical
// user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage
// and the user's selection from a menu of available mortgage loans:

// - 7 years at 5.35%
// - 15 years at 5.5%
// - 30 years at 5.75%

// Use an array for the mortgage data for the different loans. Display the mortgage payment amount followed by
// the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop
// back and enter a new amount and make a new selection or quit. Please insert comments in the program to
// document the program.

private int term;
private double rate;
private double principal;
}



Can you explain what the stack trace is?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8


Does it make sense to declare these variables inside the main and mark them private? When do they go out of scope? You declare them as private; private to what?
LeeAnne Murphy
Greenhorn

Joined: Aug 15, 2011
Posts: 18

It was placed there by Netbeans IDE 7.0, if I don't have them up there then the lower part of the code errors on me.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

If you are a beginner, I would strongly recommend agains any IDEs. That is the best way to learn Java and not the IDE.

If you move the variables, you won't get any errors, but your application won't run either because your main method will be empty.
LeeAnne Murphy
Greenhorn

Joined: Aug 15, 2011
Posts: 18

I understand that but the instructor for the class wants us to use the IDE. So that is where I am at. I will keep trying to figure it out.

Thank you for the help.
Michael Brush
Greenhorn

Joined: May 05, 2006
Posts: 4
The above posters were correct. You need to move lines 59-62 up to around line 35 to get it above the main method. You will then get the error from the IDE, but if you add to your main method, that part should be resolved. That instantiates your inner class and gives main something to do.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11419
    
  16

Did you write all this code?

If so, this is a BAD idea. I never, Never, NEVER write more than 2-3 lines before I try to compile. You have over 300. That is a recipe for disaster.

Now...

When someone said "print the stack trace", they meant "print out the exact and full text of the error you are getting." If I take your code and try to compile it from the command line (fyi, I took out the package statement to make that a little easier for me), I get the following:


C:\slop>javac Mortcalcjp2week3.java
Mortcalcjp2week3.java:54: illegal start of expression
private int term;
^
Mortcalcjp2week3.java:303: class, interface, or enum expected
public void validateUserInput(JTextField principal_txt, JTextField rate_
txt, JTextField term_txt) {
^
Mortcalcjp2week3.java:309: class, interface, or enum expected
} catch (NumberFormatException e)//Action listener
^
Mortcalcjp2week3.java:313: class, interface, or enum expected
}
^
Mortcalcjp2week3.java:318: class, interface, or enum expected
} catch (NumberFormatException e) {
^
Mortcalcjp2week3.java:322: class, interface, or enum expected
}
^
Mortcalcjp2week3.java:327: class, interface, or enum expected
} catch (NumberFormatException e) {
^
Mortcalcjp2week3.java:330: class, interface, or enum expected
}
^
8 errors

The most important thing is to always focus on the first error. There is something about that line it doesn't like. I decided to simplify things even more. I got rid of EVERYTHING except your main method. That leaves me with this:


So looking at this, you declare these variable to be private (which only makes sense instance variables), but these are method variables. Private just makes no sense here.

These variables need to either NOT be marked private, or declared outside of main, as member variables.

and as an aside, you'll notice your main method doesn't DO anything. That's probably not right either.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Grayson Churchel
Greenhorn

Joined: Aug 20, 2011
Posts: 7

I'm a noob here but I think I've spotted the problem in your code:
public static void main(String[] args) {
should be:
public static void main(String args[]) {
It's an array of String objects and not an object of type String Array.
I always like to start my debugging by looking for simple and silly mistakes I've made.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Grayson Churchel wrote:I'm a noob here but I think I've spotted the problem in your code:
public static void main(String[] args) {
should be:
public static void main(String args[]) {
It's an array of String objects and not an object of type String Array.
I always like to start my debugging by looking for simple and silly mistakes I've made.


Hi Grayson. Welcome to The Ranch!

Actually, no, that's not an error. Both are valid syntax, although personally I think String[] args is better style, as I think it better represents the meaning - i.e. "args is a variable of type array-of-Strings".

With the introduction of var-arg methods in Java 5, public static void main(String... args) is also valid.

The main error is as Fred says.
Grayson Churchel
Greenhorn

Joined: Aug 20, 2011
Posts: 7

Well this has been educational.
main(String[] args) is OK.
private variables in main is a no-no.
Good to know.
Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95

You may create an instance of your class in the main() method and put all the variables outside the main() method.
So it may look like this:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problems with this weeks code..Still not understanding Illegal start of expression...HELP PLEASE