Even Better IO: Here`s some notes on doing some even better and
Download
Report
Transcript Even Better IO: Here`s some notes on doing some even better and
Even Better IO
Getting stuff other than Strings
Problem
Most programs work with data other than
strings.
So far all we have seen is how to read strings
What do we do?
Java has an answer
Of course, Java has an answer
Java supplies methods for converting strings
to each of the built-in types.
For example to get a double value out of a
string d, use
myDouble = Double.parseDouble( d );
This of course will throw an exception if the
value in d is not a double
Other methods
All of the object types that mirror the built-in
types have methods that are similar to
parseDouble
For example, the Integer has parseInteger()
These methods require that you know what
type of value is stored within the string
Which you will have a lot of the time
But wait, how can we use these methods
You may be asking yourself, how can we use
these methods without an object around.
If you noticed in both of those last examples,
the object name started with a capital letter
That is not the usual style
Hmm…what does that mean.
Static methods
These methods that we are using are called
static methods
That means we don’t need an object of that
class to use the method.
A static method is shared among all
instances of that class, just like static
members.
Static methods can only operate on
parameters and static members.
What does this mean for me?
Now, we can actually process a file and do
stuff with what’s inside, other than count.
We can expect to open a file, read the
information out, and compute some result
based on what was inside.
This will require either knowing what is inside,
or a lot of trial and error in the code.
We will work with the former