Java Reference
In-Depth Information
BufferedReader
instantiated
Line 21
blank
insertion
point
FIGURE 3-13
The BufferedReader could be declared and constructed with two separate
lines, but it is more common to perform both actions in one line. Later you will
learn that the BufferedReader can be used to efficiently read text and numbers
from files as well as from the InputStreamReader (ISR).
Many users and textbooks create their own classes for input and output to
simplify the process of reading from the buffer. It is exactly this BufferedReader
capability, however, that makes Java easy to adapt to all kinds of input, such as
strings, numbers, special characters, and foreign language symbols. It aids in
Java's platform independence.
User Prompts, Inputs, and Conversions
As defined during program design, users of this program will respond
to prompts on the screen in order to enter the input data for height and
weight. The prompts will take the form of questions displayed with the
System.out.println() and System.out.print() methods, as shown in lines 23, 24,
25, and 28 of Figure 3-14.
After the user enters text, the readLine() method from the BufferedReader
class reads the line of inputted text and returns a String containing the contents
of the line. Lines 26 and 29 each include a readLine() method that accepts the
response from the buffer and stores it in the location named dataIn.
22
// print prompts and get input
23
System .out.println ( "\tTHE SUN FITNESS CENTER BODY MASS INDEX CALCULATOR" ) ;
24
System .out.println () ;
25
System .out.print ( "\t\tEnter your height to the nearest inch: " ) ;
26
height = dataIn.readLine () ;
27
inches = Integer .parseInt ( height ) ;
28
System .out.print ( "\t\tEnter your weight to the nearest pound: " ) ;
29
weight = dataIn.readLine () ;
30
pounds = Integer .parseInt ( weight ) ;
31
FIGURE 3-14
 
Search WWH ::




Custom Search