0

I'm trying to solve an exercise for a course I'm taking. I need to create a function that creates an array of inputs and returns the min and max values. Here's my simple code (pretty basic):

static String createVect()
{
    Scanner input = new Scanner(System.in);

    System.out.print("Number of elements: ");
    int n = input.nextInt();

    int vector[] = new int[n];      

    for (int i = 0; i < n; i++)
    {
        System.out.print("Valor " + (i+1) + ": ");
        vector[i] = input.nextInt();
    }               

    int min = Arrays.stream(vector).min().getAsInt();
    int max = Arrays.stream(vector).max().getAsInt();       

    System.out.println("Min: " + min);
    System.out.println("Max: " + max);

    return Arrays.toString(vector); 

}

public static void main(String[] args)
{
    System.out.println(createVect());   
}

It's working "ok", but I'd like to replace the System.out.print with returns but I'm not sure if I should encapsulate them or create other functions for the min and max. Thanks for the help.

1
  • 1
    You can only have one return per method, so to return multiple String's you'd have to put them in a data structure and then return the data structure Commented Aug 20, 2018 at 22:15

3 Answers 3

2

You might be interested in the summaryStatistics() method. Your method could return an instance of IntSummaryStatistics to carry the minimum and maximum (and other information). At the very least, you can use the method to avoid making multiple passes over the array:

IntSummaryStatistics stats = IntStream.of(vector).summaryStatistics();
System.out.printf("Min: %d%n", stats.getMinimum());
System.out.printf("Max: %d%n", stats.getMaximum());

For private implementation, using an array this way is quick and easy. But for a public API, it's inconvenient and error prone for clients, and can introduce to maintenance pitfalls.

Sign up to request clarification or add additional context in comments.

Comments

1

Leverage arrays to return multiple values in a single data structure:

static int[] createVect()
{
    Scanner input = new Scanner(System.in);

    System.out.print("Number of elements: ");
    int n = input.nextInt();

    int vector[] = new int[n];      

    for (int i = 0; i < n; i++)
    {
        System.out.print("Valor " + (i+1) + ": ");
        vector[i] = input.nextInt();
    }               

    int min = Arrays.stream(vector).min().getAsInt();
    int max = Arrays.stream(vector).max().getAsInt();       

    int[] returnArray; // declare returnArray as an array of integers
    returnArray = new int[2]; // create an array of 2 integers in returnArray
    returnArray[0] = min; // set the first element of returnArray to your min value
    returnArray[1] = max; // set the second element of returnArray to your max value

    return(returnArray); // return returnArray to the calling function
}

Further reading: Returning an array as return value in a method - Emory MathCS

Comments

0

You can't return different types of data in same time, you can use simply more methods which will make it easier, and by creating the vector array as instance variable.

private static int vector[]; 
public static int getMax(int [] nums){
    return Arrays.stream(nums).max().getAsInt();
}
public static int getMin(int [] nums){
    return Arrays.stream(nums).min().getAsInt();
}

public static String createVect(){
    Scanner input = new Scanner(System.in);
    System.out.print("Number of elements: ");
    int n = input.nextInt();
    vector = new int[n];
    for (int i = 0; i < n; i++) {        
        System.out.print("Valor " + (i+1) + ": ");
        vector[i] = input.nextInt();
    }               
    return Arrays.toString(vector); 
}

public static void main(String[] args) {   
    System.out.println(createVect());  
    System.out.println("The min value: " + getMin(vector)); 
    System.out.println("The max value: " + getMax(vector));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.