Java Reference
In-Depth Information
11 String text = input.nextLine();
12 System.out.println(text.toUpperCase());
13 }
14 }
15 }
This loop reads the input line by line and prints each line in uppercase until it runs
out of lines to process. It reads from a file called poem.txt . Suppose that the file has
the following contents:
My candle burns at both ends
It will not last the night;
But ah, my foes, and oh, my friends -
It gives a lovely light.
--Edna St. Vincent Millay
If you run the preceding program on this input, it will produce the following output:
MY CANDLE BURNS AT BOTH ENDS
IT WILL NOT LAST THE NIGHT;
BUT AH, MY FOES, AND OH, MY FRIENDS -
IT GIVES A LOVELY LIGHT.
--EDNA ST. VINCENT MILLAY
Notice that you could not have accomplished the same task with token-based
processing. In this example, the line breaks are significant because they are part
of the poem. Also, when you read a file token by token, you lose the spacing within
the line because the Scanner skips any leading whitespace when it reads a token.
The final line of this input file is indented because it is the name of the author
and not part of the poem. That spacing would be lost if the file was read as a series
of tokens.
String Scanners and Line/Token Combinations
In the last section we looked at a program called HoursWorked that processed the
following data file:
Erica 7.5 8.5 10.25 8 8.5
Erin 10.5 11.5 12 11 10.75
Simone 8 8 8
Ryan 6.5 8 9.25 8
Kendall 2.5 3
 
Search WWH ::




Custom Search