Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS170 Lab 8
Files and Command Line Arguments
Objectives of this lab: 
• use Scanner and File objects to read input data from files
• practice while and nested while 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  
• Start a terminal application and prepare your lab8 directory:
◦ mkdir ~/cs170/lab8
◦ cd ~/cs170/lab8
◦ 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
• 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.
• 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.
• The first command line argument will be the number of integers to read from the nums.txt 
file. 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 file
◦ 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 from every line, every other line, every third line, and so 
forth.
◦ For example,
java Lab8 6 2
reads 6 numbers by reading every other line, starting at the first line.  When run with these 
CLAs, the program should print out the value 351.
◦ Another example:
java Lab8 3 3
reads 3 numbers, 1 from every third line, starting with the first line.  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 line skip 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:
• Add your name, userid, and section number as comments to the top of your Lab8.java file.  
• Submit your Lab8.java file (not the .class file!) to Blackboard for the Lab8 assignment. 
If you do not finish during your lab period, the deadline is Saturday at 5pm.
• You can submit as many times as you wish, but we only grade the last submission.
• No late submissions for labs!