SMT.R.S.B.
ARYA VIDYA MANDIR, JUHU
Computer Applications
Std IX
Input in Java using Scanner class
We have learnt various ways to assign values to variables:
1) Using assignment operator.
Example: a=6;
Area = 3.14 * r * r;
2) Accepting value for a variable from the user at the time of execution of program but as an argument
of a function/method.
Example:
The value of r has been taken from the user as a function argument.
public class Circle
{
public void calculate(double r)
{
double area = 3.14 * r * r;
[Link](“area of circle is ” + area);
}
}
Reading values at runtime using Scanner Class.
The other way to provide initial values to the variables is to take input from the user during the execution of
the program via the output terminal. To do so, Java has a Scanner class which is predefined in the package
[Link]. You must import the [Link] package to avail the facilities available in Scanner class. the process
to import [Link] package with the Scanner class is given below:
import [Link];
OR
import [Link].*;
In the above syntax, * denotes All classes. So, specifying util.* means all the classes
available in util package can be used in the program and [Link] means only Scanner class
can be used in the program.
Creating Scanner Object
To avail the benefits of the methods available in Scanner class to read the values at runtime, we must
first create the object of Scanner class. the syntax to do so is given below:
Scanner sc = new Scanner([Link]);
1) Declare an object of class type Scanner say “sc” as given in above example. (You can use any object
name of your choice).
2) The new keyword is used to have dynamic declaration of the Scanner Object.
3) The Scanner class has a constructor which is called/invoked automatically when the object is declared
(as given in above example) to store the values in the Scanner object from the output terminal.
4) The parameter “[Link]” allows the constructor to receive data from the keyboard.
Methods to read values from Scanner object
1) nextShort();
It reads the next value from the scanner object and returns a value of short type if the entered
value is of short type.
Syntax:
short <variable> = <scannerobject>.nextShort();
Example:
Scanner sc = new Scanner([Link]);
short y = [Link]();
2) nextInt();
It reads the next value from the scanner object and returns a value of int type if the entered
value is of int type.
Syntax:
int <variable> = <scannerobject>.nextInt();
Example:
Scanner sc = new Scanner([Link]);
int y = [Link]();
3) nextFloat();
It reads the next value from the scanner object and returns the value of float type if the
entered value is of short type.
Syntax:
float <variable> = <scannerobject>.nextFloat();
Example:
Scanner sc = new Scanner([Link]);
float y = [Link]();
4) nextLong();
It reads the next value from the scanner object and returns a value of long integer type if the entered
value is of long type.
Syntax:
long <variable> = <scannerobject>.nextLong();
Example:
Scanner sc = new Scanner([Link]);
long y = [Link]();
5) nextDouble();
It reads the next value from the scanner object and returns a value of double type if the entered
value is of double type.
Syntax:
double <variable> = <scannerobject>.nextDouble();
Example:
Scanner sc = new Scanner([Link]);
double y = [Link]();
6) next();
It reads the next value from the scanner object as a string. A string which is just one word string
value. It returns and stores the value in a variable of type String. Syntax:
String <variable> = <scannerobject>.next();
Example:
Scanner sc = new Scanner([Link]);
String y = [Link]();
7) nextLine();
When an entire line of text is to be read then next() method cannot be used as it reads a string of only
one word. It doesn‟t read a string with spaces. So, to read a line of text nextLine() method is used.
Syntax:
String <variable> = <scannerobject>.nextLine();
Example:
Scanner sc = new Scanner([Link]);
String y = [Link]();
Program 1:
Define a class MenuDriven1 with suitable method that calculates and prints the result of the entered values
depending of the option selected by the user as given below:
Enter 1 to perform addition
Enter 2 to perform subtraction
Enter 3 to perform multiplication
Enter 4 to perform division
Enter your choice
Also, print “Invalid input” message if the user doesn‟t select an appropriate option.
import [Link].*;
public class Program1
{
public void main()
{
int ch, a, b;
Scanner sc = new Scanner([Link]);
[Link]("Enter 1 to perform addition");
[Link]("Enter 2 to perform subtraction");
[Link]("Enter 3 to perform multiplication");
[Link]("Enter 4 to perform division");
[Link]("Enter your choice");
ch = [Link]();
[Link]("Enter two integer values");
a = [Link]();
b = [Link]();
switch(ch)
{
case 1:
[Link]("the sum of entered values is "+ (a+b));
break;
case 2:
[Link]("the result of subtraction of entered values is "+ (a-b)); break;
case 3:
[Link]("the product of entered values is "+ (a*b));
break;
case 4:
[Link]("the result of division of entered values is "+ (a/(double)b));
break;
default:
[Link]("Sorry...Invalid Input");
}
}
}
Program 2:
Define a class with a suitable name with menu driven options that calculates and prints the result of the
value input by the user depending on the user‟s choice. The following are the options to be given:
1. To calculate absolute value
2. To calculate square root of a number
3. To print random number between 0 and 1
Also, display an error message for an incorrect option selected.
import [Link].*;
public class MenuDriven2
{
public void main()
{
int ch, a;
Scanner sc = new Scanner([Link]);
[Link]("1. To calculate absolute value");
[Link]("2. To calculate square root of a number");
[Link]("3. To print random number between 0 and 1");
[Link]("Enter your choice");
ch = [Link]();
switch(ch)
{
case 1:
[Link]("Enter an integer value");
a = [Link]();
[Link]("the absolute value of the given value is "+ [Link](a));
break;
case 2:
[Link]("Enter a value");
a= [Link]();
[Link]("the square root of the given value is "+ [Link](a));
break;
case 3:
[Link]("random value between 0 and 1 is "+ [Link]());
break;
default:
[Link]("Sorry...Invalid Input");
}
}
}
Program 3:
Define a class Fibonacci with a suitable method to print Fibonacci series of n terms; where the value
of n is to be read from the user. [Note: Fibonacci series: 0, 1, 1, 2, 3, 5, 8….]
import [Link].*;
public class Fibonacci
{
public void main()
{
int x, first=0, second=1, third, n;
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of terms");
n = [Link]();
[Link](first + "," + second);
for(x=1; x<=n-2; x++)
{
third=first+second;
[Link](","+ third);
first=second;
second=third;
}
}