Java Reference
In-Depth Information
308.2 14.9 7.4
2.8
3.9 4.7 -15.4
2.8
We think of this as a six-line file with text going across and down and two blank
lines in the middle. However, the computer views the file differently. When you typed
the text in this file, you hit the Enter key to go to a new line. This key inserts special
“new line” characters in the file. You can annotate the file with \n characters to indi-
cate the end of each line:
308.2 14.9 7.4\n
2.8\n
\n
\n
3.9 4.7 -15.4\n
2.8\n
Common Programming Error
Reading the Wrong Kind of Token
It's easy to write code that accidentally reads the wrong kind of data. For exam-
ple, the ShowSum1 program always reads exactly five doubles from the input
file numbers.dat . But suppose that the input file has some extraneous text in it:
308.2 14.9 7.4
hello
2.8
3.9 4.7 -15.4
2.8
The first line of the file contains three numbers that the program will read
properly. But when it attempts to read a fourth number, the computer finds that
the next token in the file is the text “hello”. This token cannot be interpreted as a
double , so the program generates an exception:
number 1 = 308.2
number 2 = 14.9
number 3 = 7.4
Exception in thread "main" java.util.InputMismatchException
Continued on next page
 
Search WWH ::




Custom Search