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

Thread: What is wrong with my code?

  1. #1
    Junior Member
    Join Date
    Oct 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is wrong with my code?

    I need to write the program that outputs the median of 3 numbers using a,b,c unfortunately i have an error there that says:"java:21: error: 'else' without 'if'
    else
    ^
    1 error"
    My code:
    import java.util.Scanner;

    class Main {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int a, b, c;
    System.out.print("a=");
    a = sc.nextInt();
    System.out.print("b=");
    b = sc.nextInt();
    System.out.print("c=");
    c = sc.nextInt();
    if (c<a && a<b || b<a && a<c);
    {
    System.out.println(a);
    }
    if (c<b && a>b|| a<b && c>b);
    {
    System.out.print(b);
    }
    else
    {
    System.out.print(c);
    }

    sc.close();
    }
    }


    The error is in the 21st line I tried to replace else with if It didnt work.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What is wrong with my code?

    The ; at the end of the expression following the if ends the if statement. Any thing else following that will not be part of the if.
    Remove the ; to keep from ending statement before the else

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Not sure what is wrong with my code.
    By Deeyz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2014, 05:10 AM
  2. what is wrong with my code
    By BLUEJ in forum Java Theory & Questions
    Replies: 4
    Last Post: January 11th, 2013, 04:40 PM
  3. what is wrong with my code
    By BLUEJ in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 10th, 2013, 07:52 PM
  4. Please what is wrong with my code
    By LordDavid in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 29th, 2012, 03:33 PM
  5. Please what is wrong with my code
    By LordDavid in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 28th, 2012, 09:08 PM