How can I execute this algorithm in java?

0

Hello everyone, I hope you are very well, I would like to know how I can implement this code to run it in java main:

public class InterpolationSearch {

    public static void main(String[] args) throws IOException 
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);

        System.out.println("Introduzca los numeros separados por coma");
        String[] numeros=br.readLine().split(",");
        int[] array = new int[numeros.length];

        for(int i = 0; i < numeros.length; i++) {
            array[i] = Integer.parseInt( numeros[i] );
        }

        for(int i = 0; i < numeros.length; i++) {
            System.out.print(numeros[i]+" ");
    }

    }

    static int InterpolationSearch(int []A, int x) {
        int lower_bound=0;
        int upper_bound=A.length-1;
        int index=-1;
        /* if upper bound is less than lower bound, there is not a feasible solution */
        while(lower_bound<upper_bound)
        {
            int middlepoint=lower_bound+(upper_bound-lower_bound)/(A[upper_bound] - A[lower_bound]) * (x - A[lower_bound]);
            if (x== A[middlepoint]) {
                /* x has been found */
                index=middlepoint;
                break;
            }else{
                if(x<A[middlepoint]) {
                    upper_bound=middlepoint-1;
                }else {
                    lower_bound=middlepoint+1;
                }
            }

        }

        if (lower_bound==upper_bound && A[lower_bound]==x)
        {
            index=lower_bound;
        }
        return index;

    }

    }

What I want to do is that the user can enter the numbers separated by a comma, and then enter the value you want to search.

Thank you very much in advance.

    
asked by Yesith 01.12.2017 в 00:32
source

1 answer

0

You can do it this way to get the data

String texto = JOptionPane.showInputDialog(new JFrame(), "Ingrese los valores?");
    System.out.println("Resp: "+texto);
    
answered by 02.12.2017 в 03:37