Iterators and File Input

Similar documents
Arrays - Review. Arrays as Parameters. Arrays as Parameters. Arrays and File Input. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Arrays and File Input

Arrays and File Input

Java Flow of Control

CSC 1051 Data Structures and Algorithms I

Selection Statements and operators

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Selection Statements and operators

CSC 1051 Data Structures and Algorithms I

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Lab 13. Name: Checked:

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

CSC 1051 Data Structures and Algorithms I

Selection and Repetition Revisited

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Improved algorithm. Java code. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm

Java code. Updated program. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm. Improved algorithm

Control flow, conditionals, boolean expressions, block statements, nested statements. Course website:

Introduction to Arrays

Chapter 5 Conditionals and Loops

Selection and Repetition

Selection and Repetition Revisited

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Conditionals and Loops Chapter 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Java I/O and Control Structures Algorithms in everyday life

Java I/O and Control Structures

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Arrays, Part 3: Multidimensional arrays

Algorithms and Conditionals

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

2: Basics of Java Programming

Selection and Repetition

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

File I/O and Exceptions

Using Classes and Objects

Basics of Java Programming variables, assignment, and input

ing execution. That way, new results can be computed each time the Class The Scanner

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name:

Conditional Statements

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

Happy Cinco de Mayo!!!!

Lecture 3: Variables and assignment

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

Happy Cinco de Mayo!!!!

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP File Access 1

Reading Text Files. 1 Reading from text files: Scanner

CSCI 1103: File I/O, Scanner, PrintWriter

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Text User Interfaces. Keyboard IO plus

Chapter. Let's explore some other fundamental programming concepts

Conditionals and Loops

CS111: PROGRAMMING LANGUAGE II

Question 1a) Trace of program

COMP102: Test 1 Model Solutions

I/O Streams. COMP 202 File Access. Standard I/O. I/O Stream Categories

Arrays - Review. Two-Dimensional Arrays. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

COMP102: 181. Menu. More while loops. Admin: Test. Peter Andreae

Input from Files. Buffered Reader

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Introduction to Computer Science Unit 2. Exercises

Over and Over Again GEEN163

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Recitation: Loop Jul 7, 2008

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Computational Expression

Chapter 4: Conditionals and Loops

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

ECE 122. Engineering Problem Solving with Java

CMPT 125: Lecture 4 Conditionals and Loops

Reading Input from Text File

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

Where do objects come from? Good question!

H212 Introduction to Software Systems Honors

Full file at

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

CSCI 1103: File I/O, Scanner, PrintWriter

Computational Expression

Advanced Java Concept Unit 1. Mostly Review

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

Building Java Programs

CSC 1051 Data Structures and Algorithms I. 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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

Fundamentals of Programming Data Types & Methods

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

Chapter 3. Selections

Transcription:

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

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

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

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)

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

File Input Example: FileInput.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());

File Input Example public class FileInput //---------------------------------------------------------------- sample.inp // Reads text from a file and prints it in uppercase. Computers //---------------------------------------------------------------- are useless. They can only give you answers. Pablo public Picasso static (1881 void - main 1973) (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()); Run Output COMPUTERS ARE USELESS. THEY CAN ONLY GIVE YOU ANSWERS. PABLO PICASSO (1881-1973)

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.java

//******************************************************************** // 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; continue filescan = new Scanner (new File("urls.inp"));

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();

continue Sample Run URL: www.google.com www.google.com // Read and process each line of the file while (filescan.hasnext()) URL: www.linux.org/info/gnu.html www.linux.org url = filescan.nextline(); info System.out.println gnu.html ("URL: " + url); urlscan = new Scanner (url); URL: thelyric.com/calendar/ urlscan.usedelimiter("/"); thelyric.com // Print calendar each part of the url while (urlscan.hasnext()) System.out.println URL: www.cs.vt.edu/undergraduate/about (" " + urlscan.next()); www.cs.vt.edu System.out.println(); undergraduate about URL: youtube.com/watch?v=ehcrimwrgls youtube.com watch?v=ehcrimwrgls

Another Example Input verification (Interactive input) What if input is not a number? System.out.print ("Enter the quantity: "); quantity = scan.nextint();

Example What if input is not an int? System.out.print ("Enter the quantity: "); // wrong kind of input? quantity = scan.nextint();

Example What if input is not an int? System.out.print ("Enter the quantity: "); // wrong kind of input? while (!scan.hasnextint()) // wrong kind of input? // first scan the wrong input to get rid of i String rong = scan.next(); System.out.print( Try again. Enter the quanti // now we know we have an integer, so scan it quantity = scan.nextint();

Homework Review Section 5.5 Always do all self-review exercises when you review Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus