|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
We've seen we can use
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) " ); } |
But
a
// Any (existing) filename will do: s = new java.util.Scanner( new java.io.File( "H:/itec120/lab02a/PizzaServer.java" ) ); s.next() s.next() s.next() s.next() |
However, to include this in a method, there's one caveat:
We need to tell people using our method that it's possible
that we could trigger a "file not found" error.
We do so by including the words “
class Foo { public static void FileScanDemo() throws java.io.FileNotFoundException { scnr = new java.util.Scanner( new java.io.File("/etc/motd") ); while (scnr.hasNext()) { String msg = scnr.next(); System.out.println( msg + ", echo, echo..." ); } } } |
In order to use
1 /** Open up a URL, and read the input. 2 * As a side-effect (for demonstration only), print each line to the console. 3 * @return The number of lines in a given URL. 4 */ 5 public static int countLines() throws java.net.MalformedURLException, java.io.IOException { 6 final String sourcePage = 7 "http://www.radford.edu/~itec120/2010fall-ibarland/" 8 // "http://www.radford.edu" 9 // "file:///Users/ian/120/index.html" 10 ; 11 12 // Open sourcePage for input: (Create a URL object based on sourcePage, open that URL as a stream-of-characters, 13 // and create a Scanner who will group that stream's characters into Strings etc.) 14 java.util.Scanner src = new java.util.Scanner( new java.net.URL( sourcePage ).openStream() ); 15 16 int lineCount = 0; // Keep track of how many lines we've seen. 17 String pageSoFar = ""; // All the lines of text seen so far. 18 19 while ( src.hasNext() ) { 20 pageSoFar = pageSoFar + src.nextLine(); 21 lineCount = lineCount + 1; 22 } 23 24 System.out.println( "We read:\n" + pageSoFar ); 25 return lineCount; 26 } |
java.util.Scanner s3; s3 = new java.util.Scanner( "4 score and 7 years" ); s3.hasNextInt() // If we try reading an int next, will it succeed? s3.nextInt() s3.hasNextInt() // If we try reading an int next, will it succeed? s3.next() s3.next() s3.hasNextInt() s3.nextInt() s3.hasNext() // If we try reading anything next, will it succeed? s3.next() s3.hasNext() s3.next() // Try reading anyway... |
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
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
©2012, Ian Barland, Radford University Last modified 2012.Nov.08 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |