© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements 3.6 – Iterators – p.146-150.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Lecture 4 More on Java® Data Types, Control Structures.
Introduction to Programming
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
Lecture 15: I/O and Parsing
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Chapter 1 Writing a Program Fall Class Overview Course Information –On the web page and Blackboard –
Interfaces A Java interface is a collection
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
1 Fall 2009ACS-1903 The break And continue Statements a break statement can be used to abnormally terminate a loop. use of the break statement in loops.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
CS0007: Introduction to Computer Programming File IO and Recursion.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Chapter 10 Exceptions and File I/O. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Exceptions Exception handling is an important aspect of object-oriented.
Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Program 6 Any questions?. System.in Does the opposite of System.out.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
1 BUILDING JAVA PROGRAMS CHAPTER 6 FILE PROCESSING.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
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:
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
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.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
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:
CMSC 202 Text File I/O.
Introduction to programming in java
Reading from a file and Writing to a file
Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use.
Java Course Review.
Console Input and Output
COMPUTER 2430 Object Oriented Programming and Data Structures I
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Computer Programming Methodology File Input
SELECTION STATEMENTS (1)
Outline Boolean Expressions The if Statement Comparing Data
CSC 113: Computer programming II
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Object-Oriented Design Part 2
Presentation transcript:

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

© 2011 Pearson Education, publishing as Addison-Wesley 2 Iterators  An iterator is an object that has methods that allow you to process a collection of items one at a time  The hasNext and next methods are used to loop through the collection  Several classes in the Java class library define iterator objects, including Scanner while (myCollection.hasNext()) { System.out.println(myCollection.next()); }

© 2011 Pearson Education, publishing as Addison-Wesley 3 Iterators – examples import java.util.Scanner; import java.io.*; public class URLDissector public static void main (String[ ] args) throws IOException The IOException clause in the main method header allows the computer to throw an error if there is a problem finding or opening the input file. String url; Scanner fileScan, urlScan; fileScan = newScanner (new File (“urls.inp”)); URLDissector.java (page 148) has the following code:

© 2011 Pearson Education, publishing as Addison-Wesley 4 What separates each item in an input file? You can customize the delimiter as in URLDissector.java: urlScan = new Scanner (url); urlScan.useDelimiter(“/”); By default, a Scanner object assumes that white space is the delimiter (spaces, tabs and new lines) that separates items in a file. Each iteration through the loop reads one line (one URL) from the input file and prints it out. For each URL, a new Scanner object is set up to parse the pieces of the URL string which is passed to the Scanner constructor when instantiating the URLScan object.