user9855290
user9855290

Reputation:

Java Multiple String Return

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.

Upvotes: 0

Views: 81

Answers (3)

Samir Maliqi
Samir Maliqi

Reputation: 9

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

Upvotes: 0

erickson
erickson

Reputation: 269797

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.

Upvotes: 2

esqew
esqew

Reputation: 44714

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

Upvotes: 1

Related Questions