Iterators and File Input CSC 1051 Data Structures















- Slides: 15

Iterators and File Input CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www. csc. villanova. edu/~map/1051/ Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus CSC 1051 M. A. Papalaskari, Villanova University

Outline of Chapter 5 Boolean Expressions The if Statement Comparing Data The while Statement Iterators The Array. List Class Determining Event Sources Check Boxes and Radio Buttons CSC 1051 M. A. Papalaskari, Villanova University

Iterators • Iterating: to process a collection of items, one at a time • Typical iterator methods: –has. Next() - returns true if there is at least one more item to process –next() returns the next item CSC 1051 M. A. Papalaskari, Villanova University

Iterators • Several classes in the Java standard class library are iterators • items to be processed are called tokens – Examples: words, numbers, components of a url… • The Scanner class is an iterator – the has. Next method returns true if there is more data to be scanned – the next method returns the next scanned token as a string • The Scanner class also has variations on the has. Next method for specific data types (such as has. Next. Int) CSC 1051 M. A. Papalaskari, Villanova University

Using Scanner to read from a file • Create a “file object” File my. File = new File("sample. inp"); • Create a Scanner to read from file object Scanner file. Scan = new Scanner (my. File); • Use next() to obtain next token • Use next. Line() to obtain entire line of text (until n) • Use has. Next() to test whether you are done CSC 1051 M. A. Papalaskari, Villanova University

File Input Example: File. Input. java public class File. Input { //--------------------------------// Reads text from a file and prints it in uppercase. //--------------------------------public static void main (String[] args) throws IOException { String line; File my. File = new File("sample. inp"); Scanner file. Scan = new Scanner (my. File)); // Read and process each line of the file while (file. Scan. has. Next()) { line = file. Scan. next. Line(); System. out. println (line. to. Upper. Case()); } } } CSC 1051 M. A. Papalaskari, Villanova University

File Input Example public class File. Input { //--------------------------------sample. inp // Reads text from a file and prints it in uppercase. //--------------------------------Computers are useless. They can only give you answers. public static void- main Pablo Picasso (1881 1973)(String[] args) throws IOException { String line; File my. File = new File("sample. inp"); Scanner file. Scan = new Scanner (my. File)); // Read and process each line of the file while (file. Scan. has. Next()) { line = file. Scan. next. Line(); System. out. println (line. to. Upper. Case()); } } } Run Output COMPUTERS ARE USELESS. THEY CAN ONLY GIVE YOU ANSWERS. PABLO PICASSO (1881 - 1973) CSC 1051 M. A. Papalaskari, Villanova University

Try this: What gets printed? sample 1. inp sample 2. inp //******************************** // Something. To. Do. With. Files. java Author: MAP do fa //******************************** re sol import java. util. Scanner; mi import java. io. *; public class Something. To. Do. With. Files { public static void main (String[] args) throws IOException { String line 1, line 2; Scanner file. Scan 1, file. Scan 2; file. Scan 1 = new Scanner (new File("sample 1. inp")); file. Scan 2 = new Scanner (new File("sample 2. inp")); while (file. Scan 1. has. Next() && file. Scan 2. has. Next()) { line 1 = file. Scan 1. next. Line(); line 2 = file. Scan 2. next. Line(); System. out. println (line 1 + line 2 + line 1); } System. out. println(file. Scan 1. has. Next()? "ping!": "pong!"); } } CSC 1051 M. A. Papalaskari, Villanova University

Scanner – another example: reading from a file AND from a String • Suppose we wanted to read and process a list of URLs (or other data items) stored in a file • One scanner can be set up to read each line of the input until the end of the file is encountered • Another scanner can be set up to process each line, i. e. , separating the components of each URL (at each occurrence of ‘/’) • Example: URL: www. linux. org/info/gnu. html This URL specifies a path consisting of the following components: – www. linux. org – info – gnu. html • See URLDissector. java CSC 1051 M. A. Papalaskari, Villanova University

//********************************** // URLDissector. java Author: Lewis/Loftus // // Demonstrates the use of Scanner to read file input and parse it // using alternative delimiters. //********************************** import java. util. Scanner; import java. io. *; public class URLDissector { //--------------------------------// Reads urls from a file and prints their path components. //--------------------------------public static void main (String[] args) throws IOException { String url; Scanner file. Scan, url. Scan; file. Scan = new Scanner (new File("urls. inp")); continue CSC 1051 M. A. Papalaskari, Villanova University

continue // Read and process each line of the file while (file. Scan. has. Next()) { url = file. Scan. next. Line(); System. out. println ("URL: " + url); url. Scan = new Scanner (url); url. Scan. use. Delimiter("/"); default delimiter is white space, we are changing this here // Print each part of the url while (url. Scan. has. Next()) System. out. println (" " + url. Scan. next()); System. out. println(); } } } CSC 1051 M. A. Papalaskari, Villanova University

Sample Run continue URL: www. google. com // Read and process each line of the file URL: www. linux. org/info/gnu. html while (file. Scan. has. Next()) www. linux. org { url = file. Scan. next. Line(); info System. out. println gnu. html ("URL: " + url); url. Scan = new Scanner (url); URL: thelyric. com/calendar/ url. Scan. use. Delimiter("/"); thelyric. com calendar Print each part of the url // while (url. Scan. has. Next()) System. out. println (" " + url. Scan. next()); URL: www. cs. vt. edu/undergraduate/about www. cs. vt. edu } } } System. out. println(); undergraduate about URL: youtube. com/watch? v=EHCRimw. RGLs youtube. com watch? v=EHCRimw. RGLs CSC 1051 M. A. Papalaskari, Villanova University

Another Example – Input validation (Interactive input) What if input is not a number? System. out. print ("Enter the quantity: "); quantity = scan. next. Int(); CSC 1051 M. A. Papalaskari, Villanova University

Example – What if input is not an int? System. out. print ("Enter the quantity: "); // wrong kind of input? quantity = scan. next. Int(); CSC 1051 M. A. Papalaskari, Villanova University

Example – What if input is not an int? System. out. print ("Enter the quantity: "); // wrong kind of input? while (!scan. has. Next. Int()) // wrong kind of input? { // first scan the wrong input to get rid of it String rong = scan. next(); System. out. print(”Try again. Enter the quantity: "); } // we have an integer now, so go ahead, scan it! quantity = scan. next. Int(); CSC 1051 M. A. Papalaskari, Villanova University