III.
1. Scanner class allows user to input the values of various types. It is defined within java.util
package. It breaks the input into tokens using a delimiter i.e. whitespace by default. It provides
many methods such as nextInt( ), nextDouble( ), next( ) etc. to read and parse various primitive
values.
2. Syntax: Scanner sc=new Scanner(System.in);
e.g. Scanner sc=new Scanner(System.in);
int p;
p=sc.nextInt();
It means the variable p stores an integer through the scanner object.
3. Syntax to use '?' as a token delimiter as follows:
General syntax: <Scanner Object>.useDelimiter(“delimiter”);
Since, we have to use '?' as a delimiter. Hence, syntax will be sc.useDelimiter("?");
4. nextInt() method is used to receive the next token from scanner object which can be expressed
as an integer and stored in an integer type variable.
e.g. Scanner sc=new Scanner(System.in);
int p;
p=sc.nextInt();
It means the variable p stores an integer through the scanner object.
5. hasNext Boolean() method method returns true if the next token in this scanner's input can be
interpreted as a boolean value i.e. true or false. This method returns true if and only if this
scanner's next token is a valid boolean value. Otherwise, returns false
6. Syntax of creating a PrintWriter object as follows:
PrintWriter pw=new PrintWriter(System.out.true);.
7. print( ) and println( ) methods can be applied in printWriter object to display result.
8. a. printWriter object used to write stream of characters whereas System.out.println( ) method
used to write stream of bytes.
b. printWriter flushes the stream buffer only when of the print( ) or println( ) method is called,
if automatic flushing is enabled whereas System.out.print( ) method flushes the stream buffer
whenever a newline character is written to stream, regardless of whether it was written by a
print( ) or a println( ) method.
9. java.util
10.
a. Ingeter type:- To receive integer type of data from scanner object nextInt( ) method can be
applied.
e.g. Scanner in=new Scanner(System.in);
int p;
p=in.nextInt( );
It means the variable p stores an integer through the scanner object.
b. Float Type:- To receive floating type of data from scanner object nextFloat( ) method can be
applied.
e.g. Scanner in=new Scanner(System.in);
float p;
p=in.nextFloat( );
It means the variable p stores a float type value through the scanner object.
c. Double Type:- To receive double type of data from scanner object nextDouble( ) method
can be applied.
e.g. Scanner in=new Scanner(System.in);
double p;
p=in.nextDouble( );
It means the variable p stores a float type value through the scanner object.
d. String Type:- To receive string type of data from scanner object next( ) and nextLine( )
methods can be applied.
e.g. Scanner in=new Scanner(System.in);
String p1,p2;
p1=in.next( );
p2=in.nextLine( );
It means the variable p1 stores the next token as a string whereas p2 stores a line of text
from the scanner object.
e. Boolean Type:- To receive boolean type of data from scanner object nextboolean( )
method can be applied. This method simply returns the next token of the input boolean
value.
e.g. String s = "Hello true World! 3 + 3.0 = 6 ";
Scanner sc = new Scanner(s);
while (sc.hasNext( ))
{
if (sc.hasNextBoolean( ))
{
System.out.println("Found :" + sc.nextBoolean( ));
}
System.out.println("Not Found :" + sc.next( ));
}
IV.
1.
Scanner object BufferedReader object
1. Scanner is a simple text scanner which 1. BufferedReader read text from a
can parse primitive types and strings character-input stream, buffering
using regular expressions. characters so as to provide for the
efficient reading of characters, arrays,
and lines.
2. Scanner treated given input as token. 2. BufferedReader as stream line/String.
3.
next() nextLine()
1. next( ) reads the next token. (a single 1. nextLine() reads a complete line.`
word)
2. next() positions the cursor in the same 2. nextLine() positions the cursor in the
line. next line.