r/programmingrequests • • Mar 23 '17

[Request] Basic Java program

I need a program that restricts an array to size 2 or 3 and then sort it from smallest to largest? Additionally I need the program to print an error message if the array size is not 2 or 3 Thank you for all the help I really appreciate it.

0 Upvotes

6 comments sorted by

View all comments

1

u/[deleted] Mar 23 '17

I don't understand your requirements but, Is this what you are looking for?

public  int[] arr(int[] a)
{
    //if(a.length >= 2 && a.length <= 3)
    if(a.length == 2 || a.length == 3)
    {
        Arrays.sort(a);
        return a;
    }
    //you have an error
    return null;
}

0

u/MUFC07 Mar 23 '17

I would like my program to check to see if array is an array of either length 2 or length 3. If not, print an error message and have the method return immediately. If the array is of length 2 or 3sort it from its smallest number to its largest number.

1

u/[deleted] Mar 23 '17
public  int[] arr(int[] a)
{
    //if(a.length >= 2 && a.length <= 3)
    if(a.length == 2 || a.length == 3)
    {
        Arrays.sort(a);
        return a;
    }
    //you have an error
    System.out.print("oh noes");
    return null;
}