Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Goals for today:
Scanner
and File
objects to read input data from filesScanner
breaks input into tokensFile
sSo far, we've used Scanners to read user input. Now, we're going to look at using Scanners to read over files!
A File
object allows you to interact with actual files on your computer. Make sure to put any file you want to read with a Scanner in the same folder as the program that wants to use it!
// necessary to use Files import java.io.*; // makes a File object, holding the same info as the file named "actualNameOfFile" File fileVariableName = new File("actualNameOfFile"); // a Scanner that reads over the File! Scanner fileScanner = new Scanner(fileVariableName);
Method name | Parameters | Description |
---|---|---|
exists()
|
a String file name
|
returns true if a file w/ the given file name exists in the same folder.
|
Scanner
methodsMethod name | Description |
---|---|
next()
|
reads and returns the next token as a String
|
nextLine()
|
reads and returns as a String all the characters up to
the next new line (\n )
|
nextInt()
|
reads and returns the next token as an int , if
possible
|
nextDouble()
|
reads and returns the next token as double , if
possible
|
hasNext()
|
returns true if there is still a token in
the Scanner
|
hasNextLine()
|
returns true if there is still at least one line left
to be read in the Scanner
|
hasNextInt()
|
returns true if the next token can be read as
an int
|
hasNextDouble()
|
returns true if the next token can be read as
an double
|
4.2 abc 4The following methods would read this as:
Method | What happened? | What's going on? |
---|---|---|
nextInt()
|
java.util.InputMismatchException | tried to read next token 4.2, couldn't process it as an int. |
nextDouble()
|
returns 4.2 as a double. | tried to read next token, succeeded because it could be read as a double. |
next()
|
returned "4.2" as a String. | read the next word as a String. |
nextLine()
|
returns "4.2 abc 4" as a String. | read the whole next line as a String. |
4.2 abc 4The following methods would read this as:
Method | Returned | Why? |
---|---|---|
hasNextInt()
|
false | the next token is a double, and thus cannot be read as an integer. |
hasNextDouble()
|
true | the next token is a double! |
hasNext()
|
true | the next token can be read as a String! |
hasNextLine()
|
true | there exists a line of input! |
.hasNext()
caution
.hasNext()
methods only check the type of the next token. .next()
methods consume tokens, changing what token the Scanner is looking at next! For each of the following, enter the output if the loop terminates, or write infinite
if the loop loops forever!
Consider the following as part of input.txt
Jello world :)
Scanner input = new Scanner(new File("input.txt")); while (input.hasNext()) { String nextWord = input.next(); System.out.print(nextWord + " "); } |
Jello world :) |
|
Scanner input = new Scanner(new File("input.txt")); while (input.hasNext()) { System.out.println("hi"); } |
infinite |
When you work with File
s, the Java
compiler gets concerned that you might be trying to access a File
that doesn't exist. So any method that uses File
s must declare that it might throw a FileNotFoundException: this basically tells Java that if the desired "actualNameOfFile" cannot be found, it's okay to crash.
Every method that directly or indirectly calls a method that works with File
s also needs to declare that it might throw a FileNotFoundException.
File
. Then A() and B() must declare that they might throw a FileNotFoundException.
File
. Then A() and B() and C() must all declare that they might throw a FileNotFoundException
.public static void main(String[] args) throws FileNotFoundException { ... }
.next()
vs .nextLine()
.next()
reads and returns the next token as a String. Tokens are separated by white space.
.nextLine()
reads and returns the next line as a String. A line is all the characters up to the next \n
(line break).Scanner
should only ever read its input using either .next()
(mixing with .nextDouble()
and .nextInt()
and .nextBoolean()
is fine), or using .nextLine()
.
Consider the following problem:
We already know how to read full lines or individual words from a Scanner. Now we need a new strategy: we need to read lines from the input, and then we need to somehow read word-by-word through each line.
We can do this by creating a second Scanner! So far, we've used Scanners to read user input and to read Files. Scanners can also read over Strings!
Scanner fileScannerName = new Scanner(new File("fileName")) while (fileScannerName.hasNextLine()) { String line = fileScannerName.nextLine(); // reads the next line from the input file Scanner lineScanner = new Scanner(line); while (lineScanner.hasNext()) { String word = lineScanner.next(); // reads the next word from the input line ... } }
Nice job making it this far--labs are tough! Feel free to work with the person next to you for the remaining slides. Labs are a unique opportunity (unlike homework) to collaborate directly on ideas, and practice peer programming.
These next problems get a little more challenging as we explore earlier concepts further.
We put a lot of problems in here so that you have plenty to refer back to later when working on homework. Don't feel bad if you don't finish all of them--Brett can't finish them all in a 50 minute lab, either! :)
Forest the cat says good job!
If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.
You can view an exercise, type a solution, and submit it to see if you have solved it correctly.
Choose some problems from the book and try to solve them!