RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lab12b
Reading from a file
and time to work on hw09

reading files and web pages

We've seen we can use java.util.Scanners to read input from the keyboard:

java.util.Scanner scnr = new java.util.Scanner( System.in );
System.out.print("Shout! (or ^C to indicate end-of-file) ");
while (scnr.hasNext()) {
  String msg = scnr.next();
  System.out.println( msg + ", echo, echo..." );
  System.out.print( "Shout more! (or ^C to indicate end-of-file) " );
  }
Really, a Scanner is an object who can group individual characters into meaningful units (words, doubles, etc.).

But a Scanner can read information from any source of characters: they can read information from a file, a web page, or even a String inside your program. When you call the constructor, you just pass in a source of characters.


We will use the remaining time to talk about the book's listing 7.3, and how it's similar (and, different) from what's needed on Programming Project 7.4.


1 In this case, if you try it out, you realize that web pages are written in html; in html the notion of the “next word” is different from where the next whitespace occurs. Indeed, people have written specialized classes which extend Scanner to so that the next method returns the next word in a way that is appropriate for html.      

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Nov.19 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme