for a parameter Name of the file, if in current directory • "myFile.txt" Or full name of path to the file • "C:\ics111\progs\myFile.txt" 1st command-line argument is file name (commandlineArguments[0])
new file if it does not exist, or overwrites an existing file Must have a try-catch block, as the PrintWriter constructor will throw a checked exception if the path to the file is incorrect, or if the file is open See next slide for example code
in your program on jGRASP 1.On the top menu, click Build 2.On the drop-down menu, click Run Arguments 3.A Run Arguments text field will appear at the top of the screen 4.Type your command line arguments into this text field
which has the same name as the 1st command-line argument (commandlineArguments[0]) To make the program easier to read, the main() method’s array parameter is commandlineArguments, not args If no arguments are entered, then the program ends before writing to a file
System.out is of class PrintStream, which has many methods which are similar to PrintWriter, such as print() and println() For file names, use fileName.txt, so it is easy to view the files
program that does something! We can use arrays & file I/O to implement a grocery list Think of a grocery list that you write on a sheet of paper before going to the grocery store
as an insult. All good computer scientist worship the god of modularity, since modularity brings many benefits, including the all-powerful benefit of not having to understand all parts of a problem at the same time in order to solve it.” Dr. David Clark, Computer Scientist at M.I.T.
of your code into the main() method This will make your code harder to write, harder to read, and harder to debug • You need to think about how to divide a problem into logical parts These logical parts will become your methods
saimin, or yakisoba code • Instead, you should divide your code into methods, just like a bento lunch box, which divides the food into nice, little, cute, portions…
grocery list Think of a grocery list on a sheet of paper that you usually write down before going to the grocery store Let’s break this problem down into logical parts
for our grocery list 1. Read grocery list from a file 2. Add a grocery item 3. Delete a grocery item 4. Display the grocery list 5. Write the grocery list to a file
solution to our grocery list problem, by writing methods stubs Later on, we will come back and fill in the code for each method stub • This is an example of “not having to understand all parts of a problem at the same time in order to solve it”
and men often go awry” – Robert Burns • While writing the program, I added another method to display the menu Did not exactly follow original outline • When you write programs, you may think of new methods to add on the fly as well
explain the methods (behaviors) and variables (objects) used in the program This is still a rough outline of our solution to the Grocery List problem Most method stubs display their name, so we can see if the correct methods are being called
check for possible errors in the input data • We do not want our program to crash The programmer has responsibility for creating reliable programs The programmer should NOT rely on the user to enter the correct data
errors in data input as possible 1. Enter too few command line arguments (runs OK, and has error message) 2. Enter too many command line arguments (runs OK, but needs error message)
errors in data input as possible 3. Enter choices out of range (runs OK, but needs error message) 4. Enter choices that are not Integers (program crashes!!!)
filling in the code for the other methods • Since we already wrote the code for file input and file output, let’s complete those methods next • We also need to think about the file format that we will use
is CSV (comma separated values) This is good for storing data in tables Since a CSV file is a text file, we can use any text editor, and the file is easy to read for our Java program If we want to be more sophisticated, we can use a spreadsheet application such as Microsoft Excel
into tables, so we need to think in terms of rows and columns of data The first row has the headings for each column of data, each heading separated by a comma (,) The following rows contain the actual data, each individual data in each row separated by a comma (,)
file using a simple text editor, such as WordPad or NotePad • Note the commas Then try opening the file in Excel, or some other spreadsheet application • Note that the commas have been replaced by rows and columns
badList.csv The second input file has file errors such as extra commas, missing rows, and extra data Since method readFromFile() reads a whole line at once, these errors are trivial and do not crash the program
too many rows (over 100 items) crashes the program by throwing an ArrayIndexOutOfBoundsException • We will address this issue later on in the course, as it is easy to solve using linked lists
method declaration to indicate that the method might throw an exception • Syntax for throws clause /** Example method * @exception MyException explain */ public static void method() throws MyException { //method's code }
own “fancy words” (jargon, terminology, vernacular, lingo, gibberish, gobbledygook) • Computer scientists use the word algorithm for the plan that is used to solve a problem
follow a few rules 1. The algorithm must be a detailed series of steps that will solve the problem 2. The algorithm should be simple, unambiguous, and correct 3. The algorithm can be written in many different ways
item the user wishes to delete 2. Error checking for Integer (try/catch) 3. Error checking for 0 or negative (if) 4. Error checking for too big (if) 5. If no error, remove by shifting all items on the right of deleted item one index to the left (for loop)
deleting the element (by replacing it with null) would leave empty spaces in the array Instead, we shift all the elements, so they overwrite the element to the left of them This leaves no empty array spaces
the array (for loop) 2. Output each element to the screen 1. The first element should display the headings (if statement) 2. All other elements should display the row number, and the item’s name and number (if statement)
the code for the remaining three methods Testing and correcting the code has been completed, using both good and bad data inputs, but let me know if you find any bugs
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture 4.Lighten up!