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 3 of 3

Thread: if-else-if statement

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

    Default if-else-if statement

    When i compile, i get -

    MassAndWeight.java:31: error: 'else' without 'if'
    else if(weight >= 2000)
    ^
    MassAndWeight.java:35: error: 'else' without 'if'
    else
    ^
    2 errors



    public class MassAndWeight
    {
    public static void main(String[] args)
    {
    // User input
    Scanner keyboard = new Scanner(System.in);

    double mass;

    System.out.print("Enter the object's mass: ");
    mass = keyboard.nextDouble();

    // Calculate and display
    double gravity;
    gravity = 9.8F;

    double weight;
    weight = mass * gravity;

    if (weight <= 20)
    System.out.print("The object's weight is " + weight + "Newtons.");
    System.out.print("The object is too light.");

    else if (weight <= 20)
    System.out.print("The object's weight is " + weight + "Newtons.");
    System.out.print("The object is too light.");

    else (weight <= 20)
    System.out.print("The object's weight is " + weight + "Newtons.");
    System.out.print("The object is too light.");

    }
    }

    Any help is appreciated.


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: if-else-if statement

    First of all, use <code=java></code> (<> replace with [] respectively).
    Next, try to execute this code:
    int weight = //initialize it with some number.
    if (weight <= 20)
    System.out.print("The object's weight is " + weight + "Newtons.");
    System.out.print("The object is too light.");
    Initialize weight to, say, 0, 10, 20 and 30. Explain what happens.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: if-else-if statement

    You should ALWAYS use {}s with if statements to define what statements are in the if or else statements.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  2. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  3. [SOLVED] If statement help
    By DarkPrince in forum Loops & Control Statements
    Replies: 1
    Last Post: November 7th, 2012, 04:15 PM
  4. if....else statement
    By Appu14 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 2nd, 2012, 06:35 AM
  5. If-Else Statement help
    By SnowCrashed in forum Loops & Control Statements
    Replies: 5
    Last Post: February 9th, 2010, 07:57 PM