Lecture 6 – January
27, 2005
Lab information
see methods
of Scanner class on page 90
myFileScanner = new Scanner (new
File ( “input.txt”)); // simple filename
myFileScanner = new Scanner (new
File (“C:\Temp\input.txt”)); // doesn’t work
myFileScanner = new Scanner (new
File (“C:\\Temp\\input.txt”)); // works
\ is an escape character when reading a character at a time which is
what
happens here.
Mike F suggests using the forward slash instead of the
back slash and it worked and didn’t need to be doubled.
Robert said that to avoid the
compile error message “myFileScanner may not have been initialized” he just
said myFileScanner = new Scanner (“dummyFile”); and it worked. This fooled
the compiler but may not work at runtime
Two additional things to think about
·
Which loop to use : do … while
or while
·
Should the for loop be in the
try/catch block or should the ttry/catch block be in the for loop?
Quiz – had errors
how graded
give them
template to correct Quiz2b.java
raises
questions about data types
String
arrays
Arrays
- An
array is an object which contains 0 or more variables
- If
it contains 0 variables, it is empty
- Array
variables don’t have names.
- They
are indexed by non-negative index values.
- Index
values go from 0 to n-1 where n
is the number of elements in the array.
- Array
components are homogeneous
- i.e.
an array of floats contains floats
- an
array of ints contains ints
- an
array of chars contains chars
- typical
array declarations – which do NOT create array objects
- int [
] number
- short [ ] smallNumber
- char [ ]
character
- array
initializer
- is
written as a comma-separated list of expressions enclosed by braces
{ }
- the
length of the array will equal the number of expressions
- array
declarations which DO create array objects using array initializers
- int [ ] factorial = {1,1,2,6,24,120, 720,
5040 };
- char [ ] letters = { ‘n’, ‘o’,’t’,’ ‘,’a’,’
‘,’S’,’t’,’r’,’i’,’n’,’g’};
- String [ ] words = { “array”, “of”,
“String”};
- array’s
length is available through a final instance variable length
- factorial.length
- letters.length
- words.length
- array
component access
- use
array reference followed by an indexing expression enclosed in [ ]
- all
arrays start indexing at 0
- arrays
are indexed by int values
- see: Java conversion techniques (p. 87)
- assignment
conversion
- promotion
- casting
- array
accesses are checked at runtime
- attempted
use of index less than 0 or greater than the length of the arrray causes
an ArrayIndexOutOfBoundsException
to be thrown.
- useful
information about arrays can be found at
- An
Array of characters is NOT a String
- neither
String nor an array of char is terminated by the nul
character (as it is in C)
- a String object is immutable (its
contents don’t change)
- an
array of char has mutable
components
- the
method toCharArray in class String returns an array of characters
containing the same character sequence as String
- Some
methods of the String class
Review primitive types
Enumerated types
- establish
all possible values of a variable of that type by listing or enumerating
them
- values
are identifiers
- this
means they follow the rules for identifiers
- enum Fruit { apple, berry , cherry, date};
- have
some useful methods
- name
- ordinal
- first
ordinal value is 0
- can’t
assign a numeric value to an enumerated type – why?
- Let’s
look at IceCream.java (p. 137)
Iterators
- “An
iterator is an object that has methods that allow you to process a
collection of items one at a time”
- Every
iterator object has a method called hasNext
which returns a boolean value indicating whether there is at least
one more item to process.
- What
classes have we seen that define iterator objects?