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/

Slides:



Advertisements
Similar presentations
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements 3.6 – Iterators – p
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 4510 – Machine Learning Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Chapter 5 Conditionals and Loops
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
COS 312 DAY 5 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 2 Posted – Due Feb 22 prior to class – Any issues? Assignment 3 will be posted soon Capstones.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS
Introduction to programming in java
CSC 1051 – Data Structures and Algorithms I
CSC 1051 – Data Structures and Algorithms I
Chapter 6 More Conditionals and Loops
Outline Boolean Expressions The if Statement Comparing Data
Introduction to Classes and Methods
Arrays October 6, 2006 ComS 207: Programming I (in Java)
CSC 1051 – Data Structures and Algorithms I
Outline Software Development Activities
Outline Boolean Expressions The if Statement Comparing Data
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

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

Outline of Chapter 5 Boolean Expressions The if Statement Comparing Data The while Statement Iterators The ArrayList 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: –hasNext() - 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 The Scanner class is an iterator –the hasNext 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 hasNext method for specific data types (such as hasNextInt ) CSC 1051 M.A. Papalaskari, Villanova University

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

File Input Example: FileInput.javaFileInput.java public class FileInput { // // Reads text from a file and prints it in uppercase. // public static void main (String[] args) throws IOException { String line; File myFile = new File("sample.inp"); Scanner fileScan = new Scanner (myFile)); // Read and process each line of the file while (fileScan.hasNext()) { line = fileScan.nextLine(); System.out.println (line.toUpperCase()); } } } CSC 1051 M.A. Papalaskari, Villanova University

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

Iterators The fact that a Scanner is an iterator is particularly helpful when reading input from a file Suppose we wanted to read and process a list of URLs 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 for each URL to process each part of the path See URLDissector.javaURLDissector.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 fileScan, urlScan; fileScan = new Scanner (new File("urls.inp"));urls.inp continue

CSC 1051 M.A. Papalaskari, Villanova University continue // Read and process each line of the file while (fileScan.hasNext()) { url = fileScan.nextLine(); System.out.println ("URL: " + url); urlScan = new Scanner (url); urlScan.useDelimiter("/"); // Print each part of the url while (urlScan.hasNext()) System.out.println (" " + urlScan.next()); System.out.println(); }

CSC 1051 M.A. Papalaskari, Villanova University continue // Read and process each line of the file while (fileScan.hasNext()) { url = fileScan.nextLine(); System.out.println ("URL: " + url); urlScan = new Scanner (url); urlScan.useDelimiter("/"); // Print each part of the url while (urlScan.hasNext()) System.out.println (" " + urlScan.next()); System.out.println(); } Sample Run URL: URL: info gnu.html URL: thelyric.com/calendar/ thelyric.com calendar URL: undergraduate about URL: youtube.com/watch?v=EHCRimwRGLs youtube.com watch?v=EHCRimwRGLs

Another Example – Input verification: What if input is not an int? System.out.print ("Enter the quantity: "); quantity = scan.nextInt(); 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.nextInt(); CSC 1051 M.A. Papalaskari, Villanova University

Example – What if input is not an int? System.out.print ("Enter the quantity: "); while (!scan.hasNextInt()) // 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:"); } // now we know we have an integer, so scan it quantity = scan.nextInt(); CSC 1051 M.A. Papalaskari, Villanova University

Example – What if input is negative? do { System.out.print ("Enter the quantity: "); while (!scan.hasNextInt()) // 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:"); } // now we know we have an integer, so scan it quantity = scan.nextInt(); } while (quantity <0); // try again if quantity is negative CSC 1051 M.A. Papalaskari, Villanova University

Homework Review Section 5.5 –Always do all self-review exercises when you review CSC 1051 M.A. Papalaskari, Villanova University Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus