import java.util.Vector; import java.io.*; /** * Mini program for parsing floats from Standard In (stdin, or System.in) * only 30 lines of code (comments discarded). It skips anything that * is not a number. Whitespace (horizontal tab, line feeds, carriage returns) * are used to delimit numbers (separate them from each other). You trigger * the end of input by using Ctrl-D for EndOfFile in UNIX, or Ctrl-Z in DOS. * It is trivial to change this to stop reading input upon a newline. * * @author theo the evil labby... bwahaha * @version 1.0 * @since 3/27/2001 */ public class GetFloats { /** * main method for execution. * @param argv (command-line arguments ignored) */ public static void main(String[] argv) { System.out.println(); // make your output nicely formatted. float[] arr = getFloats(); // read from stdin an array of floats System.out.println(); // another line break // print out some sensical header before just blurting out numbers. System.out.println("GetFloats read the following numbers:"); // iterate through the numbers. for(int i = 0; i < arr.length; i++) System.out.println(arr[i]); } /** * reads from System.in (Standard In) * and returns the floating point numbers. * @return an array of floating point numbers read from standard in. */ public static float[] getFloats() { // use the InputStreamReader as an adapter (hooray adapter pattern) // to convert System.in (an InputStream) into a Reader. Reader r = new InputStreamReader(System.in); // the following line is for efficiency, but isn't required. r = new BufferedReader(r); // Stream tokenizer is definitely our friend. StreamTokenizer st = new StreamTokenizer(new BufferedReader(r)); // keep track of the numbers in a Vector. // I don't believe that Vectors have been introduced in class yet, // but in a nutshell, they encompass a lot of things that you as // a comp212 student have been doing a lot recently -- a dynamic // sized structure that can hold Object elements. // see the Java API to see what Vector can do for you. Vector v = new Vector(); try { // initialize i to something you know will not be TT_NUMBER. // loop while the end of file isn't reached // use nextToken() to get the next i. for(int i = st.TT_WORD; i != st.TT_EOF; i = st.nextToken()) // if StreamTokenizer thinks it's a number. if(i==st.TT_NUMBER) // nval is by default a double. cast it to a float, // and put it in a Float wrapper for Vector insertion. v.addElement(new Float((float) st.nval)); } catch (IOException e) { // some error checking. System.err.println("Unexpected I/O Exception:"); // use the automatic text inside the Exception. System.err.println(e.getMessage()); } // create the array to return the floats in. Make its size equal to // the length of the vector now that we've parsed all the numbers. float[] f = new float[v.size()]; // convert the vector's objects for(int i = 0; i < v.size(); i++) // get the Object at spot (i), cast it as a Float, get the value. f[i] = ((Float) v.get(i)).floatValue(); // return the new float array. return f; } }