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: Don't know why this isn't working

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

    Default Don't know why this isn't working

    So I wrote this simple code to find the middle number/median of three numbers. For some reason, it is outputting 300 as the second highest/middle number though I don't see why the code would assign Array1[2] to that variable, since it doesn't meet the requirements. Any help would be appreciated.


    public class secondhighest {

    public static void main(String[] args) {
    // declares an array of integers
    int[] Array1 = {150, 75, 300};
    int secondhighest;
    secondhighest = 0;

    // tests integers in array to locate the second highest integer

    if (Array1[0] > Array1[1] && Array1[0] < Array1[2]); {
    secondhighest = Array1[0];
    }
    if (Array1[0] > Array1[2] && Array1[0] < Array1[1]); {
    secondhighest = Array1[0];
    }
    if (Array1[1] > Array1[0] && Array1[1] < Array1[2]); {
    secondhighest = Array1[1];
    }
    if (Array1[1] > Array1[2] && Array1[1] < Array1[0]); {
    secondhighest = Array1[1];
    }
    if (Array1[2] > Array1[1] && Array1[2] < Array1[0]); {
    secondhighest = Array1[2];
    }
    if (Array1[2] > Array1[0] && Array1[2] < Array1[1]); {
    secondhighest = Array1[2];
    }


    System.out.println("The second highest number is: " + secondhighest);
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Don't know why this isn't working

    Post your code in formatting tags.

    Every one of your if statements ends at the first semi-colon, ';'. Take those out and keep working on it.

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

    Eucibous (September 24th, 2013)

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

    Default Re: Don't know why this isn't working

    Aha! It was the semicolons, awesome. Thank you!

Similar Threads

  1. Why isn't my collision working?
    By dan0194 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 5th, 2013, 01:57 AM
  2. Why isn't this 2D array working?
    By alexx508 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 23rd, 2013, 07:34 AM
  3. Why isn't this code working?
    By tai8 in forum What's Wrong With My Code?
    Replies: 33
    Last Post: March 12th, 2012, 03:23 PM
  4. I don't get why this code isn't working
    By tai8 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: February 20th, 2012, 12:38 PM
  5. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 04:08 PM