Hello to all java programmers, I have a problem about method and array. The program will display the second highest value of the inputed set of numbers in the main method. The problem is that it won't accept less than four inputs which mean it is necessary to put 4 integer inputs or else it will create an error. Is there any way to make the method accepts any length from the input?
import java.io.*;
public class Test2 {
public static void Array1(int j, int k, int l, int m){
int [ ] array = new int []{j,k,l,m};
int a = -1;
int b = -1;
Integer x = null;
if(array.length <= 4) {
for (int i=0; i<4; i++){
if(array[i] > a){
b = a;
a = array[i];
}
else if (array[i] != a && array[i] > b)
b = array[i];
}
}
System.out.print("{"+array[0]+","+array[1]+","+array[2]+","+array[3]+"}");
System.out.println(" = "+b);
}
public static void main(String args[])throws IOException{
Test2.Array1(4,3,0,1);
Test2.Array1(4,3,1,1);
Test2.Array1(0,1,0,0);
Test2.Array1(1);
Test2.Array1();
}
}