Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Integer Comparison, Outputting Largest Integer Not Working

  1. #1
    Join Date
    Oct 2011
    Location
    Salt Lick City, UT
    Posts
    3
    Rep Power
    0

    Default Integer Comparison, Outputting Largest Integer Not Working

    I'm writing a Java program to take three integers and output the smallest, largest, sum, and average. Everything works except for finding the highest integer. If I set number1 to 1 and number2 to 2 and number3 to 3, the program outputs 2 as largest. If I do it in this order 3, 1, 2, it puts out 3 as the largest. The bit of code to output the smallest integer works. I include it for comparison. Thanks.

    Java Code:
            if (number1 < number2 || number1 < number3)
                smallest = number1;
            else if (number2 < number1 || number2 < number3)
                smallest = number2;
            else if (number3 < number1 || number3 < number2)
                smallest = number3;
            
            if (number1 > number2 || number1 > number3)
                largest = number1;
            else if (number2 > number1 || number2 > number3)
                largest = number2;
            else if (number3 > number1 || number3 > number2)
                largest = number3;
     
            System.out.printf("The smallest of the numbers is %d.\n", smallest);
            System.out.printf("The largest of the numbers is %d.\n", largest);

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Integer Comparison, Outputting Largest Integer Not Working

    And if you test the numbers 2, 1, 3 it outputs 2 as the smallest number; those if-statements are completely incorrect. Have a look at the Math.min( ... ) and Math.max( ... ) methods instead.

    kind regards,

    Jos

    ps. or if you insist of doing it all yourself, change the || (inclusive or) to && (logical and) operator.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Oct 2011
    Location
    Salt Lick City, UT
    Posts
    3
    Rep Power
    0

    Default Re: Integer Comparison, Outputting Largest Integer Not Working

    Thanks, JosAH.

    I didn't even know there were Math.min and Math.max methods. My book hasn't got that far into it. I'm using Deiltel's Java How to Program.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Integer Comparison, Outputting Largest Integer Not Working

    Quote Originally Posted by killingthemonkey View Post
    Thanks, JosAH.

    I didn't even know there were Math.min and Math.max methods. My book hasn't got that far into it. I'm using Deiltel's Java How to Program.
    I personally prefer the methods over a hand crafted solution; have a look, the following fragment sorts things out given the numbers a, b and c:

    Java Code:
    int max= Math.max(Math.max(a, b), c);
    int min= Math.min(Math.min(a, b), c);
    int mid= a+b+c-max-min;
    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Oct 2011
    Location
    Salt Lick City, UT
    Posts
    3
    Rep Power
    0

    Default Re: Integer Comparison, Outputting Largest Integer Not Working

    Thanks, again. That actually works better than the handcrafted version. My version threw a bad result if two of the numbers were equal to each other. Yours works correctly.

Similar Threads

  1. Working with Integer parseInt(String)
    By Rocketz in forum New To Java
    Replies: 3
    Last Post: 02-27-2011, 07:31 AM
  2. Replies: 12
    Last Post: 10-31-2010, 06:14 PM
  3. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  4. [SOLVED] Struts 2 Integer Validation not working
    By piyu.sha in forum Advanced Java
    Replies: 2
    Last Post: 10-15-2008, 01:54 AM
  5. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •