CS170 Lab 8 Arrays and Command Line Arguments Objectives of this lab: • use Scanner and File objects to read input data from files • store data read from a file into an array • practice loops and nested loops • understand and parse command line arguments Exercise Preparation: • Review Lab 7, particularly the parts about reading files. ◦ http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/file-proc.html • copy/save the file nums.txt from the class website into your lab8 directory. • If you examine the nums.txt file by typing more nums.txt you will see that the file contains many (integer) numbers, one per line. • You can press 'q' to exit the more application. Task: Read input from a file and store to an array • Goal: Write a loop which counts all the integers in the file nums.txt. This should be virtually the same as Lab 7. In fact, you can use Words.java from Lab 7 as your starting point; however, you should save your program as Lab8.java. • If your program runs correctly, at this point you'll see that nums.txt has 1000 integers in it. • In the main method, create an integer array large enough to hold all the numbers from the file. • Next, write a loop (either for or while; your choice) to store the numbers you read from the file. • You may want to print out the first 10 elements in the array (and maybe the last 10) to check and make sure the values have been stored to the array correctly. • Next, you'll modify your program to use command line arguments. Command Line Arguments: • When you run a Java program using additional parameters, the additional parameters are called “command line arguments” or CLAs for short. • Example: java MyProgram x y z ◦ x y and z are command line arguments • We can use these command line arguments to control or change the purpose of our programs. • The values of the command line arguments are passed (ie given) to a Java program through the parameter to the main method. • Remember that all Java programs require a main method in the form of: public static void main( String[] args ) { .... } ◦ args is the parameter to the main method. args has the String[] type. This means that it is an array which contains Strings. ◦ Just like any array, we can access the values in the array using the standard array notation. That is: ▪ args[0] contains the value of the 1st command line argument ▪ args[1] contains the value of the 2nd command line argument ▪ args[2] contains the value of the 3rd command line argument ▪ and so forth ◦ Regardless of how it is typed on the command line, the arguments are of type String. ▪ Example: java MyProgram 3 4 ▪ args[0] will contain the String value “3”. ▪ args[1] will contain the String value “4”. ◦ If you want to use the command line arguments to do calculations, you will need to convert them to a numerical datatype. ▪ Recall that you can use the Integer.parseInt(String value) and Double.parseDouble(String value) to convert Strings to integers or doubles. Modify your program to use the command line arguments: • Modify your program to take 2 command line arguments. It should print out an error message if the program is run without 2 arguments. Hint: check the length of args • The first command line argument will be the number of integers to read from the array. Your program should sum these numbers and print the sum. ◦ For example: java Lab8 6 1 should print out 392 which is the sum of the first 6 numbers in the array. ◦ Make sure this works before moving on. You can enter any argument you want for the 2nd command line argument for right now. • Next, modify your program to utilize the second CLA. The second argument should control whether your program sums numbers using every consecutive element in the array, every other element, every third element, and so forth. ◦ For example, java Lab8 6 2 sums 6 numbers by summing every other element in the array, starting with index 0. When run with these CLAs, the program should print out the value 351. ◦ Another example: java Lab8 3 3 reads 3 numbers, every 3rd value in the array, starting with index 0. When run with these CLAs the program should print out the value 157. • You can assume that the user of your program will always enter CLAs which can always be converted to integers. You can also assume that the user will not enter a number which is greater than the number of lines in the file, and will not request more numbers then are in the file. • Some other examples you can use to test your program: ◦ java Lab8 300 3 → 13617 ◦ java Lab8 100 4 → 5626 ◦ java Lab8 5 20 → 427 Turning in your work: • Comment your code and make sure your style is correct. • Get credit for your work by either: ◦ Submitting your work to the Lab8 assignment on Blackboard. Make sure you submit! Do not just save a draft. Submit Lab8.java (not .class!). You can submit as many times as you wish, but we only grade the last submission. ◦ Demo your working code to your TA. Make sure both you and your TA sign the signin sheet. • If you do not finish during your lab time, your submission is due to BB by 5pm on Saturday. No late submissions are accepted for labs.