Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: "Illegal Start of Expression" ?? CANT SOLVE

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default "Illegal Start of Expression" ?? CANT SOLVE

    I understand that you can't call a method within a method but I just can't see whats going on here...PLEASE HELP


    import java.util.Scanner;

    public class mg

    {
    public static void main ( String[] args)
    {
    public static double milligrams (double bodyweight)
    {

    double milligrams = bodyweight * .8;
    return milligrams;
    }

    double result1 = milligrams(a);

    System.out.println(" How much do you weigh?");
    double a = in.nextInt();
    System.out.println(" You need to take " + result1);
    }
    }
    }

    --- Update ---

    Errors :

    mg.java:8: illegal start of expression
    public static double milligrams (double bodyweight)
    ^
    mg.java:8: ';' expected
    public static double milligrams (double bodyweight)
    ^
    mg.java:8: '.class' expected
    public static double milligrams (double bodyweight)
    ^
    mg.java:8: ';' expected
    public static double milligrams (double bodyweight)
    ^
    mg.java:22: class, interface, or enum expected
    }
    ^


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: "Illegal Start of Expression" ?? CANT SOLVE

    Methods can not be defined inside of other methods. Make sure all method definitions are ended with a } before starting the definition of the next method.

    Hint: It helps keep track of the end of a method by putting a comment on the ending } with the name of the method it is ending
       } // end main()
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    ahmade (April 11th, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: "Illegal Start of Expression" ?? CANT SOLVE

    Thank you! I understand the problem but I just can't see where the problem is...

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: "Illegal Start of Expression" ?? CANT SOLVE

    Start with the definition for the first method in the class. Find the { at the beginning of its definition
    Then look for the } at the end of its definition that pairs with that beginning {.

    There should NOT be any other methods defined between the { at the beginning of the method's definition
    and the } that ends that method's definition

    Did you add comments to the }s that end each method's definition? Doing that will help you see where the method definitions are.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Proper formatting will also make it easier to see.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: "Illegal Start of Expression" ?? CANT SOLVE

    Your milligrams function is inside of your main function, move it outside of the function. You can still call it from inside the main function, you just cant define it inside the main function.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  7. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    ahmade (April 25th, 2013)

  8. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: "Illegal Start of Expression" ?? CANT SOLVE

    There are no functions in Java, goddammit!

  9. #7
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: "Illegal Start of Expression" ?? CANT SOLVE

    Congrats you call them all methods. A function returns something, a procedure does not (aka a method that returns void). Some people just cant let go of the C++ and ADA terminology. Please forgive me oh powerful one!
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. Illegal start of expression
    By bad_newbie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 16th, 2013, 08:57 PM
  2. need help with illegal start of expression
    By inshal in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 9th, 2013, 11:25 AM
  3. Help With illegal start of expression
    By RaceRed in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 7th, 2013, 09:02 PM
  4. Exception in thread "main" java.lang.IllegalArgumentException: PWC6309: Illegal compi
    By nagaraj200788@gmail.com in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 18th, 2012, 04:45 PM
  5. " Illegal start of expression" Error.
    By SanoSuke in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 21st, 2011, 08:08 PM