Looking first at creating a file using the > character. Recall the unix command cat which just dumps the contents of a file onto the screen. For instance
cat fileIf, instead, you put the command
cat file > file.outthis would copy file into file.out because the contents of file would be redirected from the console into file.out. (While amusing, this is not a good way to copy a file, use cp instead.)
cat can also be used to create files from the keyboard as shown next.
cat > file.txtTry it! After issuing this command you do not get back the Unix prompt, rather everything you type goes into a file. To stop things going into the file (and get back the Unix prompt) type <CTRL> d.
A similar thing can be done to redirect input into Java programs. In this case we use the < character on the command line. For example, consider the following Java program.
import java.util.Scanner; public class FRead { public static void main(String[] args) { Scanner s = new Scanner(System.in); while (s.hasNext()) { System.out.println(s.nextDouble()); } System.out.println("No more to read"); } }This program uses the Scanner class to read a series of doubles from System.in, which is usually the keyboard. Copy this program into a Lab6 directory. Compile and run it. Give it input from the keyboard like
5 6 7 8.0 7.7etc. You can tell the program there is no more input by typing <CTRL> d. (Exactly as you told cat there was nothing more.)
Next create a data file named data.txt with a set of numbers in it (maybe use the cat trick to do so). To get the FRead program to read this file rather than the keyboard
java FRead < data.txt
Finally, we can put this all together and have the program read a file and write to a file, all without the program knowing it is doing so. For instance
java FRead < data.txt > once.txtWhen used in this way, the FRead program does a vaguely useful task if we delete the "no more to read" line from the program. Describe what it does in your email lab submission.
In summary, I think of the redirection characters in this way: the pointy side shows where the information is going, the open side show where the information is coming from.
public static double[] readData(int maxRead) { double[] res = new double[maxRead]; Scanner s = new Scanner(System.in); int readCount = 0; while (s.hasNext() && readCount < maxRead) { res[readCount] = s.nextDouble(); readCount++; } }
With that move complete, write a new method called "average" which computes the average of the items in the array, and modify your main method to print the average. After this step, your main method should look like (I chose 6 in ReadData somewhat arbitrarily)
public static void main(String[] args) { int max = Integer.parseInt(args[0]); double[] data = readData(max); printData(data); System.out.println("Average: " + average(data)); }Now, given the command
java Stats 6 < data.txtwhere data.txt has the following contents
1.2 3 5 8 7.7 42.1 43 999.99the output of the program will be
1.2 3.0 5.0 8.0 7.7 42.1 Average: 11.166666666666666The program got a 6 from the command line (Yes you can have command line parameters and redirection) so only the first 6 items in the file were used.
public static double maximum(double[] data) { double maxx = 0; for (int i = 0; i < data.length; i++) { if (data[i] > maxx) { maxx = data[i]; } } return maxx; }
Copy this method into your program. This method has a problem; it only works correctly when the input array has at least one non-negative number. As the final part of the lab, try to fix this problem.