1. Interactive Input/Output
Contents
1. Reading data from the keyboard
2. Extracting separate data items from a String
3. Converting from a String to a primitive numerical type
4. An example showing how numerical data is read from
the keyboard and used to obtain and display a result
RAJESHREE KHANDE1
3. User input classes
RAJESHREE KHANDE3
To get user input, use the BufferedReader and
InputStreamReader classes.
The InputStreamReader class - reads the user's
input.
The BufferedReader class - buffers the user's
input to make it work more efficiently.
4. Keyboard Input
The System class in java provides an
InputStream object: System.in
buffered PrintStream object: System.out
The PrintStream class (System.out) provides support for outputting primitive
data type values.
However, the InputStream class only provides methods for reading byte
values.
To extract data that is at a “higher level” than the byte, we must “encase” the
InputStream, System.in, inside an InputStreamReader object that converts
byte data into 16-bit character values (returned as an int).
We next “wrap” a BufferedReader object around the InputStreamReader to
enable us to use the methods read( ) which returns a char value and readLine( ),
which return a String.
RAJESHREE KHANDE4
6. String Tokenizer
Consider the following program fragment:
import java.io.*;
public class TotalNumbers throws java.io.IOException{
private String str;
private int num;
public static void main (String [] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print(“Enter four integers: “);
str = br.readLine( );
str = 6 2 4 3 2 1
RAJESHREE KHANDE6
7. String Tokenizer
We need to retrieve the four separate integers from the string.
str = 6 2 4 3 2 1
A token consists of a string of characters separated by a delimiter.
Delimiters consist of {space, tab, newline, return}
A StringTokenizer parses a String and extracts the individual tokens.
StringTokenizer st = new StringTokenizer (str); //create tokenizer and pass string
String s_temp;
while (st.hasMoreTokens( ) ) {
s_temp = st.nextToken( );
//now convert this string to an integer.
RAJESHREE KHANDE7
8. StringTokenizer :Method
RAJESHREE KHANDE8
int countTokens()
Calculates the number of times that this
tokenizer's nextToken method can be called before it
generates an exception.
boolean hasMoreElements()
Returns the same value as the hasMoreTokens
method.
boolean hasMoreTokens()
Tests if there are more tokens available from this
tokenizer's string.
9. StringTokenizer :Method
RAJESHREE KHANDE9
Object nextElement()
Returns the same value as the nextToken method,
except that its declared return value is Object rather
than String.
String nextToken()
Returns the next token from this string
tokenizer.
String nextToken(String delim)
Returns the next token in this string tokenizer's
string.
10. Wrapper Classes
For each of the primitive types there is a Wrapper class.
Primitive type object
int num1 = 6; Integer myInt = Integer(num1);
double num2 = 3.1416; Double pi = Double(num2);
In the statement Integer myInt = Integer(num1); an Integer object named
myInt is created and assigned a value equal to the contents of num1
Wrapper classes begin with an uppercase letter to distinguish from their
primitive type counterpart (int, long, short, double, float, byte, char, boolean).
int Integer
double Double
float Float
char Character
RAJESHREE KHANDE10
11. Unlike primitive types, objects have operations called methods that they can
be directed to perform. (These methods have visibility static they can be
accessed by using the class name without instantiating objects of the class.
Wrapper Classes
Wrapper class objects have a method for converting a string into a primitive
type, and a method for transforming a primitive type into a string.
wrapper method name return type
Integer parseInt(String st) int
Integer toString(int num) String
Double parseDouble(String st) double
Double toString(double num) String
Float parseFloat(String st) float
Long parseLong(String st) long
RAJESHREE KHANDE11
12. Converting Tokenized String to Primitive Types
Return to the code for extracting tokens from the input string
int sum = 0, num;
String s;
while (st.hasMoreTokens( )) {
s = st.nextToken( );
//convert string to int
num = Integer.parseInt(s);
sum += num;
}
RAJESHREE KHANDE12
13. Review -- Reading stream of integers from keyboard
Step 1 – Prompt user to enter multiple integers on one line
System.out.print(“Enter four integers separated by spaces: “);
Step 2 – Retrieve keyboard input as a stream of 16-bit chars (retuned as int)
InputStreamReader isr = new InputStreamReader(System.in);
Step 3 – Form input stream of characters into a string (look for eol)
BufferedReader br = new BufferedReader(isr);
String str = br.readLine( );
Step 4 – Create StringTokenizer to extract tokens from the input string
StringTokenizer st = new StringTokenizer(str);
Need to throw java.io.IOException in function in
which it is used
RAJESHREE KHANDE13
14. Review – cont.
Step 5 – Parse the input string to extract tokens
String s1 = st.nextToken( );
//note can use while(st.hasMoreTokens( )) to repeatedly extract each
//token in the string
Step 6 – Use wrapper class methods to convert token (string) to primitive type
int num = Integer.parseInt(s1);
RAJESHREE KHANDE14
15. Putting it all together
import java.io.*; //for keyboard input methods
import java.util.*; //for StringTokenizer
public class TotalNumbers {
public static void main (String [] args) throws java.io.IOException {
String str, s;
int sum = 0, num;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print(“Enter four integers separated by spaces: “); //prompt
str = br.readLine( );
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens( )) {
s = st.nextToken( );
num = Integer.parseInt(s);
sum += num; }
}
} RAJESHREE KHANDE15