Jump to content

Java-How to sort 3 numbers without if statements

Sebastian Kurpiel

What's up guys,

I'm wondering if there is some kind of command that allows me to sort 3 integers from least to greatest. I know I could use if statements but I feel like I could condense my code with a command like this. In python I know it was like sort or something. Thanks for the help in advance.

Link to comment
Share on other sites

Link to post
Share on other sites

Use  sort() from Java.util.Arrays

int v[] = {2, 1, 9, 6, 4};
Arrays.sort(v);
for (int i : v) {
   System.out.println(i + " ");
 }

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Nineshadow said:

Use  sort() from Java.util.Arrays


int v[] = {2, 1, 9, 6, 4};
Arrays.sort(v);
for (int i : v) {
   System.out.println(i + " ");
 }

 

Thank you, I can add custom integers with the scanner correct?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Sebastian Kurpiel said:

Thank you, I can add custom integers with the scanner correct?

Yes.

 

You could also do this , but it's pretty much the same thing as using if statements :

int a , b , c;
read();
System.out.println( (a>b&&b>c?c:(a>b&&c>b?b:(b>a&&c>a?a:-1)))+ " " + (a>b&&a<c||a>c&&a<b?a:(b>a&&b<c||b>c&&b<a?b:(c>a&&c<b||c>b&&c<a?c:-1))) + " " + (a>b&&a>c?a:(c>a&&c>b?c:(b>a&&b>c?b:-1))));

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Nineshadow said:

Yes.

 

You could also do this , but it's pretty much the same thing as using if statements :


int a , b , c;
read();
System.out.println( (a>b&&b>c?c:(a>b&&c>b?b:(b>a&&c>a?a:-1)))+ " " + (a>b&&a<c||a>c&&a<b?a:(b>a&&b<c||b>c&&b<a?b:(c>a&&c<b||c>b&&c<a?c:-1))) + " " + (a>b&&a>c?a:(c>a&&c>b?c:(b>a&&b>c?b:-1))));

 

Thanks, I'm getting an error with read(); line.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Sebastian Kurpiel said:

Thanks, I'm getting an error with read(); line.

Obviously. It's a placeholder since I couldn't remember how to read stuff in Java .Just read into the numbers .

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Nineshadow said:

Obviously. It's a placeholder since I couldn't remember how to read stuff in Java .Just read into the numbers .

Oh okay lol, Thank you very much for your help. It's greatly appreciated :)

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, Nineshadow said:

Obviously. It's a placeholder since I couldn't remember how to read stuff in Java .Just read into the numbers .

Sorry to bother you again, but if I enter a small value as number three it outputs a negative 1.

import java.util.*;
import java.util.Scanner;
import java.util.Arrays;
public class Sorter
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        int a, b, c ;
        System.out.println("Hello, please enter 3 numbers so that I may sort them.");
        System.out.println("Please enter Number 1:");
        a = keyboard.nextInt();
        System.out.println("Please enter Number 2:");
        b = keyboard.nextInt();
        System.out.println("Please enter Number 3:");
        c = keyboard.nextInt();
        System.out.println( (a>b&&b>c?c:(a>b&&c>b?b:(b>a&&c>a?a:-1)))+ " " + (a>b&&a<c||a>c&&a<b?a:(b>a&&b<c||b>c&&b<a?b:(c>a&&c<b||c>b&&c<a?c:-1))) + " " + (a>b&&a>c?a:(c>a&&c>b?c:(b>a&&b>c?b:-1))));
    }
}
 

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Sebastian Kurpiel said:

Sorry to bother you again, but if I enter a small value as number three it outputs a negative 1.

import java.util.*;
import java.util.Scanner;
import java.util.Arrays;
public class Sorter
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        int a, b, c ;
        System.out.println("Hello, please enter 3 numbers so that I may sort them.");
        System.out.println("Please enter Number 1:");
        a = keyboard.nextInt();
        System.out.println("Please enter Number 2:");
        b = keyboard.nextInt();
        System.out.println("Please enter Number 3:");
        c = keyboard.nextInt();
        System.out.println( (a>b&&b>c?c:(a>b&&c>b?b:(b>a&&c>a?a:-1)))+ " " + (a>b&&a<c||a>c&&a<b?a:(b>a&&b<c||b>c&&b<a?b:(c>a&&c<b||c>b&&c<a?c:-1))) + " " + (a>b&&a>c?a:(c>a&&c>b?c:(b>a&&b>c?b:-1))));
    }
}
 

I would highly suggest using an array for this, sorting, and then displaying it. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Sebastian Kurpiel said:

System.out.println( (a>b&&b>c?c:(a>b&&c>b?b:(b>a&&c>a?a:-1)))+ " " + (a>b&&a<c||a>c&&a<b?a:(b>a&&b<c||b>c&&b<a?b:(c>a&&c<b||c>b&&c<a?c:-1))) + " " + (a>b&&a>c?a:(c>a&&c>b?c:(b>a&&b>c?b:-1))));

If this is for school, I wouldn't recommend going with this method. It looks like a copy/paste so it may lose you marks/get you a zero depending on if that kind of thing is considered cheating or not. The marker likely won't believe you wrote this yourself. If they asked you to explain it to them, could you?

 

Also, even if they wouldn't lower your marks for that, if the work is graded in any way on readability it's likely to do poorly.

 

If it's not for school/doesn't matter then I guess you're free to do whatever.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×