Sample Test Run
text.text
contains a header value, followed by that many lines of text. Create an array of strings of the
appropriate size, read in the lines of text into the array — one line of text per element, and print out the elements of the
array in reverse each element preceded by it subscript. Don't forget to use nextLine
so that an entire line is
read in — that will also permit blanks in the text.
Sample Test Run
For example if the file text.text
contains:
4 Arrays allow the programmer to create collection of objects The size of an array can be obtained in Java using the .length field For loops are the loop of choice for processing an array The first element of an array has an index of 0the program should produce the following output:
3. The first element of an array has an index of 0 2. For loops are the loop of choice for processing an array 1. The size of an array can be obtained in Java using the .length field 0. Arrays allow the programmer to create collection of objectsNote:There is a bit of an issue involved in reading in the above file (and in general, mixing
nextLine
calls
with the other nextXXX
calls). You can read about it in section 3.10 'The Scanner Bug' of the text; here is an
overview, and the way to handle it:
nextIn
(which you use to get the number of text lines in the file), the
scanner is still on the same line as the integer
4
.
nextLine
causes the scanner to input the rest of the next line (which, given that
we are at the end of the line, consists of nothing, or what we will soon learn is called the 'empty string'
nextLine
right after the nextInt
4
, and position itself at
the beginning of the second line.
nextLine
which will read in the second line (as desired).
newLine
and Scanner
's
other input methods