Page 85 - CA_Blue( J )_Class10
P. 85
nextInt() It scans the next Integer type value from the terminal.
nextLine() It scans the complete line of text from the terminal.
nextLong() It scans the next Long type value from the terminal.
nextShort() It scans the next Short type value from the terminal.
The above methods of the Scanner class are used to convert the input value into different primitive data types.
Following are some syntax to take different types of values:
To accept a word:
String w=sc.next();
To accept a sentence:
String s=sc.nextLine();
To accept an Integer:
a. Integer of size 1 byte:
byte b= sc.nextByte();
b. Integer of size 2 bytes:
short s= sc.nextShort();
c. Integer of size 4 bytes:
int n= sc.nextInt();
d. Integer of size 8 bytes:
long l= sc.nextLong();
To accept a real number:
a. Real number of size 4 bytes:
float f= sc.nextFloat();
b. Real Number of size 8 bytes:
double d= sc.nextDouble();
To accept a character:
char c= sc.next().charAt(0);
To find the volume of a cube, a sphere and a cuboid by input the required values from user using
Program 5
Scanner class.
1 import java.util.*;
2 class scanner
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc= new Scanner(System.in);
7 int s,l,b,h,vc,vcd;
8 double r,vs;
9 System.out.print("Enter side of a cube in cm : ");
10 s=sc.nextInt();
11 vc=s*s*s;
12 System.out.println("Volume of a cube : "+vc + " cu cm ");
13 System.out.println("-----------------------");
83
Input in Java 83

