Java program – A simple filter

by Nideesh C on April 28, 2011 · 0 comments

in Java Programming




public class RangeFilter {
    public static void main(String[] args) {

        // read in two command-line arguments
        int lo = Integer.parseInt(args[0]);
        int hi = Integer.parseInt(args[1]);

        // repeat as long as there's more input to read in
        while (!StdIn.isEmpty()) {

            // read in the next integer
            int t = StdIn.readInt();

            // print out the given integer if it's between lo and hi
            if (t >= lo && t <= hi) {
                StdOut.print(t + " ");
            }
        }
        StdOut.println();
    }
} 


/*************************************************************************
 *  Execution:    java RangeFilter lo hi < input.txt
 *  Dependencies: StdIn.java
 *
 *  Read in a sequence of integers from standard input and print
 *  out those values between lo and hi.
 *
 *************************************************************************/



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Java program – Averaging a stream of numbers
  2. Java program – Interactive user input
  3. Java program – Converting to binary
  4. Java program – Sampling without replacement
  5. Java program – Casting to get a random integer

Leave a Comment

Previous post:

Next post: