Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Intro to Programming & Algorithm Design Data Files Assg This presentation can be viewed on line in a file named: Copyright 2003 by Janson Industries ch09.IntrotoProg.Files.ppt 1 Objectives Explain Advantages How of files to process files Show how to use files in Java 2 Copyright 2014 by Janson Industries File Everything on a computer is stored in files Images Documents Web pages Spreadsheet SFC and Raptor flowcharts Java source code Java bytecode 3 Copyright 2014 by Janson Industries Data File We are talking about data files Inventory data Customer data Sales data Kind of like an array but more permanent When pgm stops data still exists 4 Copyright 2014 by Janson Industries File Advantages Instead of entering data every time a pgm is run, user can enter once and pgm can save to a file Data can then be accessed at a later time This is how apps remember info Browser favorities Amazon purchase history Video game high scores 5 Copyright 2014 by Janson Industries Data Files Are stored on secondary storage Hard drive Flash drive Cloud drive A program can put data into a file (write to the file) or retrieve a copy of the data from the file (read the file) 6 Copyright 2014 by Janson Industries Files Programs read files just like they read data from the command line Can read the entire line Can read one word i.e. All the text up to the next space Programs write to files just like they write to the console With a carriage return Without carriage return 7 Copyright 2014 by Janson Industries Files When writing, some languages will overwrite any existing data in a file Others let you append data Before a file can be written to or read from, it must be opened Any data written to a file will be saved when the file is closed by the progam 8 Copyright 2014 by Janson Industries Files In pseudocode you must declare a program file name At the same time the file must also be defined as input or output Lastly, the program file name has to be associated with the file's physical name 9 Copyright 2014 by Janson Industries Files Windows file's physical names have a prefix and a suffix Prefix can have many parts (separated by periods) but the suffix indicates the type of info in the file Dear.John.docx My.First.Pgm.java My.Latest.Greatest.Selfie.jpeg 10 Copyright 2014 by Janson Industries Writing to Files The program file name and the file's physical name are associated in the open statement Declare OutPutFile customerFile Open customerFile "Customer.dat" Write customerFile "Joe Customer" Write customerFile "1 Main St." Write customerFile "Enid, OK 34343" Close customerFile 11 Copyright 2014 by Janson Industries Reading Files Declare the input program file name Associate it with the physical file name Read the file Results in the 3 variables holding the file data Copyright 2014 by Janson Industries Declare String custName, custStreet, custCSZ Declare InputFile customerFile Open customerFile "Customer.dat" Read customerFile custName Read customerFile custStreet Read customerFile custCSZ Close customerFile 12 Appending to Files In the declare, identify the mode as AppendMode So, running the following Declare OutputFile AppendMode customerFile Open customerFile "Customer.dat" Write customerFile "Mary Buyer" Write customerFile "2 Oak St Write customerFile "Muncie, IN 54545" Close customerFile Copyright 2014 by Janson Industries Results in 13 Using Loops to Process Files Like arrays, loops very useful for accessing data in files Unlike arrays, files can have variable amounts of data No Copyright 2014 by Janson Industries length variable for files Most file systems have a way to determine if the end of the file has been reached 14 Using Loops to Process Files Copyright 2014 by Janson Industries In pseudocode, use the eof (end of file) function eof(customerFile) will return a true if the last record/line has been read Can use eof in a while loop to process all the data in a file 15 Using Loops to Process Files Copyright 2014 by Janson Industries Assuming Customer.dat has the following info 16 Using Loops to Process Files To display a mailing list like this: Joe Customer 1 Main St. Enid, OK 34343 Mary Buyer 2 Oak St. Muncie, IN 54545 Terry Smith 3 Pine Ave. Palatka, FL 32273 Pat Jones 4 Lake Dr. Erie, PA 23232 Copyright 2014 by Janson Industries 17 Using Loops to Process Files Module main() Declare Integer ctr Declare String tempVariable Declare InputFile customerFile Open customerFile "Customer.dat" While NOT eof(customerFile) For ctr = 1 to 3 Read customerFile tempVariable Display tempVariable End For Display " " End While Close customerFile End Module 18 Copyright 2014 by Janson Industries Using Loops to Process Files Declare variables and open the file 19 Copyright 2014 by Janson Industries Using Loops to Process Files 20 Copyright 2014 by Janson Industries Files in Raptor To read, still use the Input function but must first identify the file as the source of the input I.e. not the keyboard/console Done by calling the Redirect_Input function Redirect_Input("E:/Customer.dat") Copyright 2014 by Janson Industries To write to a file use the Redirect_Output function 21 Using File Loops in Raptor Raptor has an End_Of_Input function that is used to control a loop To close a file, set the redirect to false Redirect_Input(false) Copyright 2014 by Janson Industries 22 Using File Loops in Raptor EOF loop to read all records Loop to read the three records for each customer Copyright 2014 by Janson Industries 23 Using File Loops in Raptor Reads and displays a record Blank line separating each customers data Close the file Copyright 2014 by Janson Industries 24 Using File Loops in Raptor Results Copyright 2014 by Janson Industries 25 Files in Java To read, still use a Scanner and its next(), nextLine, nextDouble, etc. methods Need to tie the scanner to the file Done when creating the Scanner Scanner customerFile = new Scanner(new File("E:/Customer.dat")); Copyright 2014 by Janson Industries Need to import java.io.File; 26 Files in Java If the file doesn't exist, creating the scanner will cause a File Not Found Exception Two ways to handle Put the following code in the header of the method public static void main(String[] args) throws IOException { And Copyright 2014 by Janson Industries import java.io.IOException; 27 Files in Java Or you can enclose the statement as follows try { customerFile = new Scanner(new File("E:/Customer.dat")); } catch (FileNotFoundException e) { e.printStackTrace(); } Copyright 2014 by Janson Industries And import java.io.FileNotFoundException; 28 Using File Loops in Java The Scanner has a method called hasNext Will return true if there is more data To close a file, use the Scanner's close method customerFile.close(); Copyright 2014 by Janson Industries 29 import import import public java.io.File; java.io.IOException; java.util.Scanner; class FileLoop { Using File Loops in Java public static void main(String[] args) throws IOException { //Declare variables int ctr; String tempVariable = new String(); //Declare objects and variables needed to read the file Scanner customerFile = new Scanner(new File("E:/Customer.dat")); while(customerFile.hasNext()) { for(ctr = 1; ctr <4; ctr++) { //Reads and displays one record from the file tempVariable = customerFile.nextLine(); System.out.println(tempVariable); } //Blank line for readablity System.out.println(""); } //Close the file customerFile.close(); } } 2014 by Janson Industries Copyright 30 Using File Loops in Java Copyright 2014 by Janson Industries 31 Modifying Files Text files not always easy to add to or modify That's Copyright 2014 by Janson Industries why there are Databases Some languages allow you to append to the file but some don't But no language has an easy way to modify 32 Modifying Files To add when language doesn't allow adding, have to: Read all the records from the file Write them to a temp file Get the new data and write it to the temp file Then read all the records from the temp file Write I.e. Copyright 2014 by Janson Industries them to the original file write over what was in the original file 33 Modifying Files - Adding Customer.dat Old Data New User Data Old and New Data Copyright 2014 by Janson Industries AddPgm Old Data TempFile.dat New Data AddPgm AddPgm Old and New Data 34 Adding a Record Fortunately, Java allows appending so we won't have to mess with the temporary file when adding But Raptor doesn't!! Also, we should check the file and make sure that a record doesn't already exist for the "new" data Have to search the entire file before adding a record Copyright 2014 by Janson Industries 35 Modifying a Record User specifies record to modify Read and write all the records from original file to temp until the record to modify is read Prompt the user with the old data Get new data and write it to temp Copyright 2014 by Janson Industries Read rest of original records and write them to temp Copy temp back to original 36 Modifying a Record User Data To Modify AddPgm Data AddPgm Data TempFile.dat Records Records Up to the Up to the one to modify one to modify Customer.dat Old Data AddPgm User New Data Copyright 2014 by Janson Industries New Data 37 Modifying a Record Rest of Rest of Customer.dat AddPgm TempFile.dat Data Data Customer.dat All Data AddPgm All Data TempFile.dat 38 Copyright 2014 by Janson Industries Modifying Files Will design a program to allow users to add, modify or display a customer's information If user wants to add a customer, will check that info for that customer does not already exist Copyright 2014 by Janson Industries If info does exist, will not allow user to add 39 Module main() Declare String option = "" While option NOT equal 4 Display "What would you like to do? Enter:" Display "1 to add a customer" Display "2 to modify a customer" Display "3 to display a customer" Display "4 to end the pgm" Input option If option = "1" Then addRecord() Else If option = "2" Then modifyRecord() Else If option = "3" Then displayRecord() End If End If End If End While End Module Modifying Files Copyright 2014 by Janson Industries 40 Module addRecord() Declare String custToAdd Declare String custNameInFile Declare String tempVariable Declare Boolean recordExists = false Modifying Files custToAdd = getCustomerName() Declare InputFile customerFile Open customerFile "Customer.dat" While NOT eof(customerFile) AND recordExists = false Read customerFile custNameInFile If custNameInFile = custToAdd Then recordExists = true Else For ctr = 1 to 2 Read customerFile tempVariable End For End If End While Close customerFile Copyright 2014 by Janson Industries 41 Modifying Files If recordExists = true Then Display "Sorry there is already a record for customer " + custToAdd Else Declare OutputFile AppendMode customerFileOut Open customerFileOut "Customer.dat" Write customerFileOut custToAdd Display "What is the customers street address" Input tempVariable Write customerFileOut tempVariable Display "What is the customers city, state and zip" Input tempVariable Write customerFileOut tempVariable Display "Customer " + custToAdd + " added" Close customerFileOut End If End Module Copyright 2014 by Janson Industries 42 Module modifyRecord() Declare String custToMod Declare String custNameInFile Declare String tempVariable Declare Boolean recordExists = false custToMod = getCustomerName() Declare OutputFile tempFile Open tempFile "TempFile.dat" Declare InputFile customerFile Open customerFile "Customer.dat" While NOT eof(customerFile) AND recordExists = false Read customerFile custNameInFile Write tempFile custNameInFile Read till record to If custNameInFile = custToMod Then change found recordExists = true Else For ctr = 1 to 2 Read customerFile tempVariable Write tempFile tempVariable End For End If 43 End While Modifying Files Copyright 2014 by Janson Industries Modifying Files If record not found If recordExists = false Then Display "Sorry there is no record for customer " + custToMod Close customerFile Else Read customerFile tempVariable Display "This is the customers current street address " Display tempVariable Display "What is the customers new street address" Input tempVariable If record found get new info and write it to Write tempFile tempVariable temFile Read customerFile tempVariable Display "This is the customers current city, state and zip" Display tempVariable Display "What is the customers new city, state and zip" Input tempVariable Write tempFile tempVariable Display "Customer " + custToMod + " changed" 44 End If Copyright 2014 by Janson Industries Modifying Files While NOT eof(customerFile) Read customerFile tempVariable Write rest of customer records to tempFile Write tempFile tempVariable End While Close customerFile Close tempFile Declare InputFile tempFileIn Open tempFileIn "TempFile.dat" Declare OutputFile customerFileOut Open customerFileOut "Customer.dat" While NOT eof(tempFileIn) Read tempFileIn tempVariable Write customerFileOut tempVariable End While Write all records to Close customerFileOut Customer from tempFile Close tempFileIn End Module Copyright 2014 by Janson Industries 45 Module displayRecord() Declare String custToDisp Declare String custNameInFile Declare String tempVariable Declare Boolean recordExists = false custToDisp = getCustomerName() Declare InputFile customerFile Open customerFile "Customer.dat" While NOT eof(customerFile) AND recordExists = false Read customerFile custNameInFile Read till record to If custNameInFile = custToDisp Then display found recordExists = true Display custNameInFile When found display For ctr = 1 to 2 the 3 records Read customerFile tempVariable Display tempVariable End For Display " " End If End While Displaying Files Copyright 2014 by Janson Industries 46 Modifying Files If recordExists = false Then Display "Sorry there is no customer " + custToDisp End If Close customerFile End Module Function String getCustomerName() String custName Display "What is the customers name" Input custName Return custName End Function Copyright 2014 by Janson Industries 47 main method Modifying Files SFC Display the options Input the option Copyright 2014 by Janson Industries 48 Modifying Files SFC Call method based on option Copyright 2014 by Janson Industries 49 Modifying Files SFC addRecord method Loop checking if customer already exists Copyright 2014 by Janson Industries 50 Modifying Files SFC addRecord method Checking customer name No match: read the next two records so positioned at next name Copyright 2014 by Janson Industries 51 addRecord method Match: display msg no add done Copyright 2014 by Janson Industries Modifying Files SFC No match: prompt user to supply data, read, write to customer file 52 addRecord method Modifying Files SFC Close the file Copyright 2014 by Janson Industries 53 modifyRecord method Modifying Files SFC Set up files Copyright 2014 by Janson Industries 54 modifyRecord method Modifying Files SFC Loop searching for customer name No match: read the next two records so positioned at next name Copyright 2014 by Janson Industries 55 modifyRecord method No match no change Copyright 2014 by Janson Industries Modifying Files SFC Match: read cust data and prompt user for new data 56 modifyRecord method Modifying Files SFC Write new data to temp Finish writing rest of cust data to temp Copyright 2014 by Janson Industries 57 modifyRecord method Modifying Files SFC Close files Re-open so temp input and customer is output Copyright 2014 by Janson Industries 58 modifyRecord method Modifying Files SFC Copy all temp records to customer Copyright 2014 by Janson Industries 59 displayRecord method Modifying Files SFC Set up variables, get name, open file Start loop to search file Copyright 2014 by Janson Industries 60 displayRecord method Modifying Files SFC If customer found, display 3 records and a blank line Copyright 2014 by Janson Industries 61 displayRecord method Modifying Files SFC Msg if customer not found getCustomerName method Copyright 2014 by Janson Industries 62 Modifying Files Raptor Because of Raptor's problem with accepting numeric characters as strings, will make option an Integer Can't append, so will have to work with temp file to do an Add Copyright 2014 by Janson Industries This means opening the TempFile when we open Customer As we search to see if customer already exists, write each record to temp 63 main method Modifying Files Raptor Display options and input option Call correct function Copyright 2014 by Janson Industries 64 addRecord method Modifying Files Raptor Loop checking if customer already exists Copyright 2014 by Janson Industries 65 addRecord method Modifying Files Raptor Checking customer name No match: write name and next two records to the temp file Close customer file Copyright 2014 by Janson Industries 66 addRecord method Match: display msg no add done Modifying Files Raptor No match: write name to temp prompt user to supply data, read, write to temp file Close temp, then open temp as input and customer as output Copyright 2014 by Janson Industries 67 addRecord method Modifying Files Raptor Write all records from temp to customer Close files and display user msg Copyright 2014 by Janson Industries 68 Modifying Files Raptor In Raptor, when you are writing to a file, can't display info in console So in modifyRecord: Copyright 2014 by Janson Industries You could but this means the redirect must be set to false and the next write will overwrite the file Won't prompt with current data Must get all the new data before redirecting Will need two more variables to hold the data 69 Modifying Files Raptor Alternatively, we could: Copyright 2014 by Janson Industries Read the entire file looking for record Retrieve the data Then read the entire file again and write to the temp file, etc. Would mean a extra read of the entire file… 70 modifyRecord method Modifying Files SFC Get new info Set up files Copyright 2014 by Janson Industries Loop to find record to be changed 71 modifyRecord method Check if record found Modifying Files Raptor No match: write cust name and next two records to temp Copyright 2014 by Janson Industries 72 Modifying Files Raptor Match: write new customer data No match: no change Match: skip rest of old customer data Match: write rest of data from customer to temp Copyright 2014 by Janson Industries 73 Close files Re-open so temp is input and customer is output modifyRecord method Modifying Files Raptor Write all data from temp to customer Copyright 2014 by Janson Industries 74 modifyRecord method Modifying Files Raptor Close files Display user msg Copyright 2014 by Janson Industries 75 displayRecord method Modifying Files Raptor Loop to search for record to display Copyright 2014 by Janson Industries 76 If record found display the name… Modifying Files Raptor displayRecord method …get and display next 2 records with rest of data Copyright 2014 by Janson Industries 77 displayRecord method Modifying Files Raptor Display blank line If record was not found display user msg Close file Copyright 2014 by Janson Industries 78 Modifying Files Raptor getCustomerName method Copyright 2014 by Janson Industries 79 Modifying Files - Raptor Given this data Copyright 2014 by Janson Industries 80 Modifying Files Raptor Copyright 2014 by Janson Industries Running the flowchart results in: 81 Modifying Files Raptor Copyright 2014 by Janson Industries Specifying new customer info: 82 Modifying Files Raptor Changing customer info: Copyright 2014 by Janson Industries 83 Modifying Files Raptor Copyright 2014 by Janson Industries 84 Modifying Files Raptor And this is the data at the end Copyright 2014 by Janson Industries 85 Modifying Files Java To write to a file you use a PrintWriter Specify the file name and location when creating the PrintWriter object Like you did with the Scanner PrintWriter tempFile = null; tempFile = new PrintWriter("E:/TempFile.dat"); Copyright 2014 by Janson Industries Have to enclose in try/catch or specify throws IOException in method header 86 Modifying Files Java To append to a file you also use a PrintWriter But you create a FileWriter object and specify the file name and the value true PrintWriter customerFileOut = null; customerFileOut = new PrintWriter(new FileWriter("E:/Customer.dat", true)); Copyright 2014 by Janson Industries 87 Modifying Files Java To write you use the PrintWriters print or println methods customerFileOut.println("Frank Smith"); String custName = "Joe Blow" customerFileOut.print(custName); println writes data to a new line in the file print writes to the same line Frank SmithJoe Blow Copyright 2014 by Janson Industries 88 Modifying Files in Java import import import import import import java.io.File; java.io.FileNotFoundException; java.io.FileWriter; java.io.IOException; java.io.PrintWriter; java.util.Scanner; public class FileMod { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { main method String option = ""; // Display menu of options while (!option.equals("4")) { System.out.println("What would you like to do? Enter:"); System.out.println("1 to add a customer"); System.out.println("2 to modify a customer"); System.out.println("3 to display a customer"); System.out.println("4 to end the pgm"); 89 = keyboard.nextLine(); Copyright 2014 byoption Janson Industries // Invoke correct method based on option selected if (option.equals("1")) { addRecord(); } else { if (option.equals("2")) { modifyRecord(); } else { if (option.equals("3")) { displayRecord(); } } } } Modifying Files in Java } public static void addRecord() { addRecord method // Variables to hold file data String custToAdd, custNameInFile, tempVariable; boolean recordExists = false; // Variables for reading and writing to the customer file Scanner customerFile = null; 90 PrintWriter customerFileOut = null; Copyright 2014 by Janson Industries // Retrieve the customer name to add custToAdd = getCustomerName(); // Create the scanner object to read the customer file try { customerFile = new Scanner(new File("E:/Customer.dat")); } catch (FileNotFoundException e) { e.printStackTrace(); } // Read every record and compare every third record //with the new name while (customerFile.hasNext() && recordExists == false) { custNameInFile = customerFile.nextLine(); if (custNameInFile.equals(custToAdd)) { recordExists = true; } else { for (int ctr = 1; ctr < 3; ctr++) { tempVariable = customerFile.nextLine(); } } } 91 customerFile.close(); Copyright 2014 by Janson Industries Modifying Files in Java // If customer name is already in file, don't add if (recordExists == true) { System.out.println("Sorry there is already a record for customer " + custToAdd); } else { // Create PrintWriter object to append to customer file try { customerFileOut = new PrintWriter(new FileWriter( "E:/Customer.dat", true)); } catch (IOException e) { e.printStackTrace();} // Append the new name, address and CSZ to the customer file customerFileOut.println(custToAdd); System.out.println("What is the customers street address"); tempVariable = keyboard.nextLine(); customerFileOut.println(tempVariable); System.out.println("What is the customers city, state and zip"); tempVariable = keyboard.nextLine(); customerFileOut.println(tempVariable); System.out.println("Customer " + custToAdd + " added"); customerFileOut.close(); } 92 End of addRecord method } 2014 by Janson Industries Copyright public static void modifyRecord() { String custToMod, custNameInFile, tempVariable; boolean recordExists = false; modifyRecord method custToMod = getCustomerName(); // Variables and objects for reading and writing // to the customer and temp files Scanner tempFileIn = null; Scanner customerFile = null; PrintWriter tempFile = null; PrintWriter customerFileOut = null; try { customerFile = new Scanner(new File("E:/Customer.dat")); tempFile = new PrintWriter("E:/TempFile.dat"); } catch (FileNotFoundException e) { e.printStackTrace(); } // Read the customer file and write the info to the temp // file until the customer to modify is found while (customerFile.hasNext() && recordExists == false) { custNameInFile = customerFile.nextLine(); tempFile.println(custNameInFile); if (custNameInFile.equals(custToMod)) { 93 recordExists = true; Copyright 2014 by Janson Industries } else { for (int ctr = 1; ctr < 3; ctr++) { tempVariable = customerFile.nextLine(); tempFile.println(tempVariable); } } modifyRecord method } // If the customer name not in file, don't allow change if (recordExists == false) { System.out.println("Sorry there is no record for customer " + custToMod); customerFile.close(); } else { // Get the current customer info and display it // Read the new info and write it to the temp file tempVariable = customerFile.nextLine(); System.out.println("This is the customers current street address "); System.out.println(tempVariable); System.out.println("What is the customers new street address "); tempVariable = keyboard.nextLine(); 94 tempFile.println(tempVariable); Copyright 2014 by Janson Industries tempVariable = customerFile.nextLine(); System.out.println("This is the customers current city, state and zip "); System.out.println(tempVariable); System.out.println("What is the customers new city, state and zip "); tempVariable = keyboard.nextLine(); tempFile.println(tempVariable); System.out.println("Customer " + custToMod + " changed"); // Write the rest of the customer records to the temp file while (customerFile.hasNext()) { tempVariable = customerFile.nextLine(); tempFile.println(tempVariable); } // Close the files the create new objects to // read the temp file and write to the customer file customerFile.close(); modifyRecord method tempFile.close(); try { tempFileIn = new Scanner(new File("E:/TempFile.dat")); customerFileOut = new PrintWriter("E:/Customer.dat"); } catch (FileNotFoundException e) { 95 Copyright 2014 by Jansone.printStackTrace();} Industries // Copy all the temp file records to the customer file while (tempFileIn.hasNext()) { tempVariable = tempFileIn.nextLine(); customerFileOut.println(tempVariable); } customerFileOut.close(); End modifyRecord tempFileIn.close(); method } } // Display a particular customers record. // Uses same logic as FileLoop. displayRecord method public static void displayRecord() { String custToDisp, custNameInFile, tempVariable; boolean recordExists = false; Scanner customerFile = null; custToDisp = getCustomerName(); try { customerFile = new Scanner(new File("E:/Customer.dat")); } catch (FileNotFoundException e) { 96 e.printStackTrace(); } Copyright 2014 by Janson Industries while (customerFile.hasNext() && recordExists == false) { custNameInFile = customerFile.nextLine(); if (custNameInFile.equals(custToDisp)) { recordExists = true; System.out.println(custNameInFile); for (int ctr = 1; ctr < 3; ctr++) { tempVariable = customerFile.nextLine(); System.out.println(tempVariable); } System.out.println(""); } } if (recordExists == false) { System.out.println("Sorry there is no customer " + custToDisp); } customerFile.close(); End displayRecord method } public static String getCustomerName() { String custName; System.out.println("What is the customers name"); custName = keyboard.nextLine(); return custName; getCustomerName } method } Copyright 2014 by Janson Industries 97 Modifying Files - Java Given this data Copyright 2014 by Janson Industries 98 Modifying Files - Java Choose add Add new customer Display newly added customer info Copyright 2014 by Janson Industries 99 Modifying Files - Java Choose modify Change some of the data Display changed customer info Copyright 2014 by Janson Industries 100 Modifying Files - Java Test that all checks work Try to add an already existing customer Try to change and display a nonexisting customer Copyright 2014 by Janson Industries 101 Modifying Files - Java And this is the data at the end Copyright 2014 by Janson Industries 102 Processing File Data Files can hold both text and numbers Instead of using parallel arrays can use files to hold different types of info 103 Copyright 2014 by Janson Industries Processing Data And a language like Java lets you read individual values not just the whole record next() gets all text up to the next space nextInt() gets all text up to the next space and converts it to an integer nextDouble() gets all text up to the next space and converts it to a double All of these do not advance the cursor to the next line! Copyright 2014 by Janson Industries 104 Processing Data Not nitpicking! If file had the following budget data Rent 500 Car 280 Utilities 175 And you tried to process with these statements While (not eof) name = infile.nextLine() amount = infile.nextDouble() End While Copyright 2014 by Janson Industries 105 Processing Data There would be an exception the second time nextDouble executed WHAAAAAAAT!! Must show where cursor is after each statement executed to understand First nextLine() reads "Rent" and places cursor at the beginning of the second line in the file 106 Copyright 2014 by Janson Industries Processing Data When nextDouble executed 500 read and cursor placed at end after the second 0 Rent 500 Car 280 Utilities 175 Cursor after first read Cursor after second read When the second nextLine executed it reads the rest of the second line A Copyright 2014 by Janson Industries big fat null 107 Processing Data And moves cursor to beginning of third line Cursor after 3rd read (2nd nextLine) Rent 500 Car 280 Utilities 175 Now when second nextDouble executed InputMismatchException Tries to convert "Car" into a double 108 Copyright 2014 by Janson Industries Processing Data Solution: Execute another nextLine after the nextDouble Forces the cursor to the line after the line with the double value While (not eof) name = infile.nextLine() amount = infile.nextDouble() infile.nextLine() End While 109 Copyright 2014 by Janson Industries Processing Data When using files like this, there is generally a file specification document which Identifies each field of data with a Name Location within the file Size (optional) Data type 110 Copyright 2014 by Janson Industries Processing Data So we could have stored the customer data like this The file specification would look something like this… 111 Copyright 2014 by Janson Industries Processing Data File Name: customer.dat Description: contains the name and address of each customer Field Description Customer first name Customer last name Street number Street name Street type City State Zip code Data Type String String String String String String String String 112 Copyright 2014 by Janson Industries Processing Data Storing data this way could cause problems What if street name is comprised of multiple words 23 Pond View Dr. 14 Puppy Dog Tr. 2300 Martin Luther King Hwy How do you know how many reads to do? Could add a new field that has the street name length (number of words) 113 Copyright 2014 by Janson Industries Processing Data Joe Customer 1 1 Main St Enid, OK 34343 Mary Buyer 2 1 Oak St. Muncie, IN 54545 Terry Smith 3 1 Pine Ave. Palatka, FL 32273 Pat Jones 4 1 Lake Dr. Erie, PA 23232 Frank Smith 5 1 Minerva La Ronkonkoma, NY 11783 Joe Blow 23 2 Pond View Dr. Santa Barbara, CA 99999 Sue Banks 14 2 Puppy Dog Tr. Walla Walla, WA 97654 Al Adams 2300 3 Martin Luther King Hwy El Paso, NM 83536 Uh-oh what about city now? This is why text files are of limited use and DBMS are so important 114 Copyright 2014 by Janson Industries Processing Data Control breaks are interuptions in normal program execution Like when a record was modified Normal processing was copying the data from the customer file to the temp file When the customer to change was reached, different instructions were performed • The new info was retrieved from the user and written to the temp file Then the copying continued Control breaks used frequently when printing reports Copyright 2014 by Janson Industries 115 Control Breaks Example, a report that has the following format: Report For header (on first page) each subsequent page A page header with page number For next 33 items • Print line item (from file data) A page footer with page number Report footer (on last page) 116 Copyright 2009 by Janson Industries Control Breaks Wisconsin State Sales Report xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx : : : : xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx : : : : xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx : : : : xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx Detailed Wisconsin Sales Report – page 2 xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx : : : : xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx Copyright 2009 by Janson Industries xxxx xxxx xxxx xxxx xxxx xxxx xxxx : : : : xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx : : : : xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx : : : : xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx : : : : xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx : : : : xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx : : : : xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx : : : : xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx End of page 2 xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx : : : : xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx : : : : xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx : : : : xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx : : : : xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx : : : : xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx 117 Control Breaks xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx : : : : xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx : : : : : : : : xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx xxxxxxxx xxxxxxx Detailed Wisconsin Sales Report – page 3 xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx : : : : xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx : : : : xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx : : : : : : : : xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx : : : : xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx : : : : xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx : : : : xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx : : : : : : : : : : : : : : : : xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx xxxxxxxx xxxxxxx xxxxxxx xxxx End of page 3 xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx : : : : : : : : xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx End of Wisconsin State Sales Report 118 Copyright 2009 by Janson Industries Control Breaks Module main() Declare Integer pageCtr = 1, lineCtr = 0 Declare String reportHeader = "Wisconsin State Sales Report " Declare String pageHeader = " Detailed Wisconsin Sales Report – page " Declare String pageFooter = " End of page ", tempVariable Display reportHeader Skip to next page pageCtr = pageCtr + 1 // Declare and open the file Declare InputFile salesFile Open salesFile "sales.dat" // Priming read Read salesFile tempVariable 119 Copyright 2009 by Janson Industries Use Nested Loops Outer Loop While (not eof) Display pageHeader, pageCtr While (lineCtr < 33 AND not eof) Display tempVariable Inner lineCtr = lineCtr + 1 Loop Read salesFile tempVariable End While lineCtr = 0 Display pageFooter, pageCtr Skip to next page pageCtr = pageCtr + 1 End While Display reportFooter End Module Copyright 2009 by Janson Industries 120 Points to Remember Files can hold many values of differing types Hold data permanently Progam ends but data is still in the file Just as with arrays, easiest to process files with loops 121 Copyright 2014 by Janson Industries Assignments Non-Graded Chap 9 labs 9.1-9.3 Graded Chap 9 lab 9.4 122 Copyright 2014 by Janson Industries