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;
}

0

u/MUFC07 Mar 23 '17

This is what i have so far public static void main (String[] args){

			int length;
			
			Scanner scnr=new Scanner(System.in);
			
			System.out.println("How many values are you going to enter?");
			length = scnr.nextInt();

			if(length<2 || length>3){
				System.out.println("Error");
				return;
			}
			int[] values = new int[length];
			for(int counter = 0; counter <length; counter++){
				System.out.print("Enter values: ");
				values[counter] = scnr.nextInt();
				System.out.println("My new array is " + Arrays.toString(values));
			}
			System.out.println("Now the Array is " + Arrays.toString(values)); 
		}
		void sortArray(int[] values) {
	        int[] sorted = values.clone();
	        for (int i = 0; i < sorted.length; i++) {
	            for (int j = i+1; j < sorted.length; j++) {
	                if ( (sorted[i] > sorted[j]) && (i != j) ) {
	                    int temp = sorted[j];
	                    sorted[j] = sorted[i];
	                    sorted[i] = temp;
		
		
	}
	            }
	            
	        }
	        }
		}

1

u/[deleted] Mar 23 '17 edited Mar 23 '17

Is this what you are looking for?

import java.util.*;

class number{

    public static void main (String[] args){
        int length;

        Scanner scnr=new Scanner(System.in);

        System.out.println("How many values are you going to enter?");
        length = scnr.nextInt();

        if(length < 2 || length > 3){
            System.out.println("Error");
            return;
        }
        int[] values = new int[length];
        for(int counter = 0; counter <length; counter++){
            System.out.print("Enter values: ");
            values[counter] = scnr.nextInt();
            System.out.println("My new array is " + Arrays.toString(values));
        }
        Arrays.sort(values);
        System.out.println("Now the Array is " + Arrays.toString(values));
    }
}