Reading Inputs
Dr. Alekha Kumar Mishra
Reading Input from Console
●
[Link] refers to the standard output device and
[Link] to the standard input device.
●
Console input is not directly supported in Java
●
However, we can use the Scanner class to create an
object to read input from [Link], as follows:
– Scanner input = new Scanner([Link]);
●
The object of Scanner class may invoke its
methods.
Dr. Alekha Kumar Mishra
Method for Scanner class object
●
nextByte() reads an integer of the byte type.
●
nextShort() reads an integer of the short type.
●
nextInt() reads an integer of the int type.
●
nextLong() reads an integer of the long type.
●
nextFloat() reads a number of the float type.
●
nextDouble() reads a number of the double type.
●
next() reads a string that ends before a whitespace character.
●
nextLine() reads a line of text (i.e., a string ending with the Enter
key pressed).
Dr. Alekha Kumar Mishra
Example
import [Link].*; // Scanner is in the [Link] package
public class ConsoleInput{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter a number for radius: ");
double radius = [Link]() ;
double area = radius * radius * 3.14159;
[Link]("The area for the circle of radius " +radius + " is " + area);
}
}
Dr. Alekha Kumar Mishra
Another way
●
To obtain a character-based stream that is attached to the
console, wrap [Link] in a BufferedReader object.
●
BufferedReader supports a buffered input stream.
●
To creates a BufferedReader that is connected to the
keyboard:
– BufferedReader br = new BufferedReader(
new InputStreamReader([Link]));
●
Here, br is a character-based stream that is linked to the
console through [Link].
Dr. Alekha Kumar Mishra
Reading Characters
import [Link].*;
class BRRead {
public static void main(String args[]) throws IOException{
char c;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter characters, 'q' to quit.");
do {
c = (char) [Link]();
[Link](c);
} while(c != 'q');
}
}
Dr. Alekha Kumar Mishra
Reading Strings
import [Link].*;
class BRReadLines {
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
String str;
[Link]("Enter lines of text.");
[Link]("Enter 'stop' to quit.");
do {
str = [Link]();
[Link](str);
}while();
}
}
Dr. Alekha Kumar Mishra
Using Command-Line
Arguments
●
The command-line arguments inside a Java program are
stored as strings in a String array, args which is passed as the
parameter to main().
●
The first command-line argument is stored at args[0], the
second at args[1], and so on.
class CommandLine {
public static void main(String args[]) {
for(int i=0; i<[Link]; i++)
[Link]("args[" + i + "]: " + args[i]);
}
}
Dr. Alekha Kumar Mishra
Variable length argument
(classical style)
class PassArray {
static void vaTest(int v[]) {
[Link]("Number of args: " + [Link] +" Contents: ");
for(int x : v)
[Link](x + " ");
[Link]();
}
}
public static void main(String args[]){
// array created to hold the arguments.
int n1[] = { 10 };
int n2[] = { 1, 2, 3 };
int n3[] = { };
vaTest(n1); // 1 arg
vaTest(n2); // 3 args
vaTest(n3); // no args
}
}
Dr. Alekha Kumar Mishra
Variable length argument
(varargs feature)
●
A variable-length argument is specified by three periods (...).
●
For example, function vaTest( ) is written using a vararg:
– static void vaTest(int ... v) {
●
This syntax tells the compiler that vaTest( ) can be called with
zero or more arguments.
●
As a result, v is implicitly declared as an array of type int[ ].
●
Thus, inside vaTest( ), v is accessed using the normal array
syntax
●
You can overload a method that takes a variable-length
argument
Dr. Alekha Kumar Mishra
Example varargs
class VarArgs {
// vaTest() now uses a vararg.
static void vaTest(int ... v) {
[Link]("Number of args: " + [Link] + " Contents: ");
for(int x : v)
[Link](x + " ");
[Link]();
}
public static void main(String args[]) {
vaTest(10); // 1 arg
vaTest(1, 2, 3); // 3 args
vaTest(); // no args
}
}
Dr. Alekha Kumar Mishra
Restriction on varargs
●
The varargs parameter must be last in the
argument list and there must be only one
varargs parameter.
●
int doIt(int a, int b, double c, int ... vals) {
●
int doIt(int a, int b, double c, int ... vals, boolean
stopFlag) { // Error!
●
int doIt(int a, int b, double c, int ... vals, double ...
morevals) { // Error!
Dr. Alekha Kumar Mishra
Another example
class VarArgs2 {
static void vaTest(String msg, int ... v) {
[Link](msg + [Link] + " Contents: ");
for(int x : v)
[Link](x + " ");
[Link]();
}
public static void main(String args[]) {
vaTest("One vararg: ", 10);
vaTest("Three varargs: ", 1, 2, 3);
vaTest("No varargs: ");
}
}
Dr. Alekha Kumar Mishra