This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes 'else' without 'if' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Watch " New topic

'else' without 'if'

J Morberg

Joined: Mar 02, 2003
Posts: 5
Hello. I'm compiling my first program which
  • accepts 3 numbers from the user
  • sorts ascending
  • prints 3 numbers in ascending order

  • I am having a problem with my if/else statements
    "'else' without 'if'"
    else if (y > z); on line 28
    else if (x > y); on line 33
    here is the code:

    This is my first program, so obviously I don't know exactly what I am doing. Any help would be appreciated.
    Thank you so much,
    Chad McGowan
    Ranch Hand

    Joined: May 10, 2001
    Posts: 265
    Remove the semi-colon from your if and else if statements. Here's the proper syntax for an if statement
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    Also, you are missing the closing braces for the if block and for the else if block. Use the example above to see where the closing braces should go.
    Keep coding!

    Java API Documentation
    The Java Tutorial
    J Morberg

    Joined: Mar 02, 2003
    Posts: 5
    Layne and Chad,
    Thank you both for your help. Your suggestions quickly fixed my problem.
    The code compiled w/out error...but now it wont execute!
    When trying to run my "Sort.class" file, I receive the following error:
    Exception in thread "main" java.lang.NoClassDefFoundError: Sort
    Hopefully this one is equally as simple to fix!
    Thanks again ~ JM
    Rajeshwari Natarajan
    Ranch Hand

    Joined: Mar 05, 2003
    Posts: 67
    the code runs fine for me..are u using the following command??

    regards<br />Rajeshwari. N
    J Morberg

    Joined: Mar 02, 2003
    Posts: 5
    Thank you for checking the code out. Yes, I am using the right command.
    I believe I might have some classpath or something set up wrong. I am going to investigate that!

    I keep getting the error
    Exception in thread "main" Java.lang.NoClassDefFoundError: Sort
    my classpath is set to include the directory for my sdk installation; a file residing in a j2rel.4.0 directory; and a servlet.jar file within a tomcat directory I've installed to run the server.
    As long as one of the classpath entries is set to the "j2sdk1.4.1_01" directory I should be fine, right?
    Does it seem that a missmatch in my classpath could create such a problem?
    oh, by the way...if it worked for you, then I feel much more confident that it's just a setting on my machine that is causing the problem. I will research and find out how to fix it. Thanks again for checking it!
    Take care/JM
    Rajeshwari Natarajan
    Ranch Hand

    Joined: Mar 05, 2003
    Posts: 67
    From which directory are u running the program?
    U shud be running the java class from the directory in which the .class file is created.
    If u are running from jdk1.4\bin or any other folder then the directory containing the class file must be included in the classpath.
    If sample is the name of the directory containing Sort.class then give the following command..

    and then run ur program.
    It is sorta covered in the JavaRanch Style Guide.
    subject: 'else' without 'if'
    Similar Threads
    Need help with algorithm
    user input
    Simple Logic Question
    Currency Problem + General Advice