Csc 1051 data structures and algorithms i
This presentation is the property of its rightful owner.
Sponsored Links
1 / 16

CSC 1051 – Data Structures and Algorithms I PowerPoint PPT Presentation


  • 80 Views
  • Uploaded on
  • Presentation posted in: General

CSC 1051 – Data Structures and Algorithms I. Iterators and File Input. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

Download Presentation

CSC 1051 – Data Structures and Algorithms I

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Csc 1051 data structures and algorithms i

CSC 1051 – Data Structures and Algorithms I

Iterators and File Input

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

Outline of Chapter 5

Boolean Expressions

The ifStatement

Comparing Data

The whileStatement

Iterators

The ArrayListClass

Determining Event Sources

Check Boxes and Radio Buttons

CSC 1051 M.A. Papalaskari, Villanova University


Iterators

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


Iterators1

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

Using Scanner to read from a file

  • Create a “file object”

    File myFile = newFile("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 java

File Input Example: FileInput.java

publicclass FileInput{//----------------------------------------------------------------// Reads text from a file and prints it in uppercase.//----------------------------------------------------------------publicstaticvoid 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 filewhile (fileScan.hasNext()) { line = fileScan.nextLine(); System.out.println (line.toUpperCase()); } }}

CSC 1051 M.A. Papalaskari, Villanova University


File input example

File Input Example

publicclass FileInput{//----------------------------------------------------------------// Reads text from a file and prints it in uppercase.//----------------------------------------------------------------publicstaticvoid 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 filewhile (fileScan.hasNext()) { line = fileScan.nextLine(); System.out.println (line.toUpperCase()); } }}

sample.inp

Computers are useless. They can only give you answers.Pablo Picasso (1881 - 1973)

Run Output

COMPUTERS ARE USELESS. THEY CAN ONLY GIVE YOU ANSWERS. PABLO PICASSO (1881 - 1973)

CSC 1051 M.A. Papalaskari, Villanova University


Iterators2

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

  • SeeURLDissector.java

CSC 1051 M.A. Papalaskari, Villanova University


Csc 1051 data structures and algorithms i

//********************************************************************

// 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"));

continue

CSC 1051 M.A. Papalaskari, Villanova University


Csc 1051 data structures and algorithms i

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


Csc 1051 data structures and algorithms i

Sample Run

URL: www.google.com

www.google.com

URL: www.linux.org/info/gnu.html

www.linux.org

info

gnu.html

URL: thelyric.com/calendar/

thelyric.com

calendar

URL: www.cs.vt.edu/undergraduate/about

www.cs.vt.edu

undergraduate

about

URL: youtube.com/watch?v=EHCRimwRGLs

youtube.com

watch?v=EHCRimwRGLs

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


Another example input verification what if input is not an int

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

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 int1

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

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

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

CSC 1051 M.A. Papalaskari, Villanova University


  • Login