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

        }
    }

If you put the input to the method in an array, then you will be able to pass any any sized array you want to the method.
If you pass the values separately then the number of parameters to the method is set when you define the method.
If you define it with 4 parameters then you must give it 4 arguments when you call it.

Is there any way to make the method accepts any length from the input?

Tecnically yes, you can use Array1(int... n) which declares a variable nunber (>=0) of int params - inside your method you will see n as an int array of the appropriate size.
But in practice most people would use an array as Norm suggests because in the calling program if there is a variable number of ints they will probably be in some kind of array anyway.

Tecnically yes, you can use Array1(int... n) which declares a variable nunber (>=0) of int params - inside your method you will see n as an int array of the appropriate size.
But in practice most people would use an array as Norm suggests because in the calling program if there is a variable number of ints they will probably be in some kind of array anyway.

can you show me how to do it, I tried what you said but I am confused on how.

    public static void Array1(int j, int k, int l, int m){
vs
    public static void Array1(int[] anArray) {

How can I use? I made my Array1 goes like this:

public static void Array1(int[] array){

        int a = -1;
        int b = -1;

        for (int i=0; i<array.length; 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);
        }

and the error is:

Test2.Array1(4,3,0,1);
Test2.Array1(4,3,1,1);

If you declare the method as having an array as its parameter then you MUST pass an array to it when you call it, eg

    int[] data = {4.3.0.1);
    Test2.Array1(data);

ps there's a method that does what you hand-coded on line 15 - Arrays.toString(array) - which works the same with arrays of any length or type.

System.out.print(Arrays.toString(array));

the code did worked but when I changed the input to

int[] data = {4,3,1);

there is no error but when run the program, the output goes like this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

I think this is due to 3 inputs. I also try the

System.out.print(Arrays.toString(array));

but the error is on the Arrays.toString();

You probably still have a hard-coded array size or index somewhere in your program
What's the error in Arrays.toString - did you import java.util.Arrays ?

(ps - I'm leaving now - no more posts today)

Thank you so much, I already solve the problem. I forgot to import the java.util. . hehehe sorry but it is okay now. Thank you. . Now it is solve.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.