• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 18
Netbeans IDE Firefox Browser Windows Vista
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So where are you getting the error? Can you post the stack trace?
 
LeeAnne Murphy
Greenhorn
Posts: 18
Netbeans IDE Firefox Browser Windows Vista
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
Netbeans IDE Firefox Browser Windows Vista
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
Netbeans IDE Firefox Browser Windows Vista
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
Android Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
Android Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this has been educational.
main(String[] args) is OK.
private variables in main is a no-no.
Good to know.
 
Ranch Hand
Posts: 95
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic