Java Reference
In-Depth Information
Implement a
split
method that returns an array of
String
contain-
ing the tokens of the
String
. Use a
Scanner
. The method signature
for
split
is
2.25
public static String [ ] split( String str )
An alternative to using a
Scanner
is to use the split method for a
String
. Specifically, in the statement below:
2.26
String [ ] arr = str.split( "\\s" );
if
str
is
, then
arr
will be an array of length four stor-
ing the
Strings
"
this
"
,
"
is
"
,
"
a
"
, and
"
test
"
. Modify the code in
Section 2.6.2 to use
split
instead of a
Scanner
.
"
this is a test
"
2.27
Both a
Scanner
and
split
can be configured to use delimiters that are
different than the normal whitespace. For instance, in a comma sepa-
rated file, the only delimiter is the comma. For split, use
"[,]"
as
the parameter, and for
Scanner
, at the statement
scan.useDelimiter( "[,]" )
Armed with this information, modify the code in Section 2.6.2 to work
for a comma separated line of input.
PROGRAMMING PROJECTS
2.28
Create a data file
double1.txt
containing floating point numbers and
suitable for use in Exercise 2.14. Write a method that invokes the
functions in that exercise on the data in your file. Make sure there is
only 1 item per line and handle all problems.
Create a data file
double2.txt
containing floating point numbers in a
two dimensional array suitable for use in Exercise 2.15. Write a
method that invokes the functions in that exercise on the data in your
file. If your code in Exercise 2.15 requires that the two-dimensional
array be rectangular, then throw an exception if the data file does not
represent a rectangular array, prior to invoking your method.
2.29
Create a data file
double3.txt
containing floating point numbers in a
two dimensional array suitable for use in Exercise 2.15. The numbers
in each row should be comma separated. Write a method that invokes
the functions in that exercise on the data in your file. If your code in
Exercise 2.15 requires that the two-dimensional array be rectangular,
then throw an exception if the data file does not represent a rectangu-
lar array, prior to invoking your method.
2.30
Search WWH ::
Custom Search