1
votes

I apologize for a rookie question. I am trying to run the Java programs in Eclipse given in the Algorithms, 4th edition book by Robert Sedgewick and Kevin Wayne: https://algs4.cs.princeton.edu/home/

I am having trouble with input arguments to the programs.

For example for the following program:

import java.util.Arrays;

public class BinarySearch
{
    public static int rank(int key, int[] a)
    { // Array must be sorted.
     int lo = 0;
        int hi = a.length - 1;
        while (lo <= hi)
        { // Key is in a[lo..hi] or not present.

          int mid = lo + (hi - lo) / 2;
            if (key < a[mid]) hi = mid - 1;
            else if (key > a[mid]) lo = mid + 1;
            else return mid;
        }
        return -1;
    }



    public static void main(String[] args)
    {

        int[] whitelist = In.readInts(args[0]);
        Arrays.sort(whitelist);
        while (!StdIn.isEmpty())
        { // Read key, print if not in whitelist.        
         int key = StdIn.readInt();
            if (rank(key, whitelist) < 0)
            StdOut.println(key);
        }    
    }
}

The input arguments are:

% java BinarySearch tinyW.txt < tinyT.txt

I don't know where to pass input arguments in Eclipse. Any help will be appreciated.

3
Go to Run->Run As-> Run configuration ... there you will see Command line arguments text box where you can add the argumentsmettleap
int key = StdIn.readInt(); This will not work. Unless you use the lib.Khan Saab
Since your program relies on redirection, you'll have to open a cmd window and run Java from the command line. IDEs do not support file redirection.RealSkeptic
@RealSkeptic I tried in cmd. But it throws an error. The "<" in the command is interfering somehow.cuRiOusBOT
Then add the error to your question, please.RealSkeptic

3 Answers

3
votes

Go in "Run configuration..." opening the menu of the play button. Picture

You find what you need in arguments, environment tab and common. Actually common is the tab that you need.

1
votes

Thanks everyone! I solved it. Apparently the newer libraries provided on the book site vary a bit from the book that I have.

I changed the main function as follows:

import java.util.Arrays;
public class BinarySearch{
public static int rank(int key, int[] a)
{ // Array must be sorted.
    int lo = 0;
    int hi = a.length - 1;
    while (lo <= hi)
    { // Key is in a[lo..hi] or not present.
        int mid = lo + (hi - lo) / 2;
        if (key < a[mid]) hi = mid - 1;
        else if (key > a[mid]) lo = mid + 1;
        else return mid;
    }
    return -1;
}

public static void main(String[] args)
{
    In i = new In(args[0]);
    In j = new In(args[1]);
    int[] whitelist = i.readAllInts();
    int[] iplist = j.readAllInts();
    Arrays.sort(whitelist);
    for (int key:iplist)
    { // Read key, print if not in whitelist.
        if (rank(key, whitelist) < 0)
        StdOut.println(key);
    }
}
}

Then pass

"tinyW.txt" "tinyT.txt"

as Program arguments as indicated by @GDG612 .

0
votes

Right click on your program and select Run As -> Run Configurations... Then click on (x)= Arguments tab and pass the inputs to the program under Program arguments