Java Dr.
Nabanita Das
Scanner Class
In Java, Scanner is a class in java.util package used for obtaining the input of the primitive
types like int, double, etc. and strings.
Methods used for extracting data are mentioned below:
Method Description
nextBoolean() Used for reading Boolean value
nextByte() Used for reading Byte value
nextDouble() Used for reading Double value
nextFloat() Used for reading Float value
nextInt() Used for reading Int value
nextLine() Used for reading Line value
nextLong() Used for reading Long value
nextShort() Used for reading Short value
Example 1:
// Java program to read data of various types
// using Scanner class.
import java.util.Scanner;
public class ScannerDemo1 {
// main function
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// String input
String name = sc.nextLine();
Java Dr. Nabanita Das
// Numerical data input
// byte, short and float can be read
// using similar-named functions.
int age = sc.nextInt();
long mobileNo = sc.nextLong();
double cgpa = sc.nextDouble();
// Print the values to check if the input was
// correctly obtained.
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Mobile Number: " + mobileNo);
System.out.println("CGPA: " + cgpa);
}
}
1. Java Program to Swap Two Numbers Using User-defined Function and take
input from user
import java.util.Scanner;
public class SwapNumber
{
public static void swap(int a, int b)
{
//swapping logic
a = a + b;
b = a - b;
a = a - b;
System.out.println(" After Swapping x = "+a+" y = "+b);
}
public static void main(String args[])
{
int x, y;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
x = sc.nextInt();
System.out.print("Enter the second number: ");
y = sc.nextInt();
System.out.println("Before Swapping x = "+x+" y = "+y);
swap(x, y);
}
}
Java Dr. Nabanita Das
2. Print a factorial of a number in Java.
import java.util.*;
class Factorial {
public static void main(String[] args) {
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
//Declare and Initialize the variable
System.out.println("Enter the number: ");
int num=sc.nextInt();
int fact=1;
for(int i=1;i<=num;i++)
{
fact=fact*i;
}
System.out.println("Factorial of the number: "+fact);
}
}
3. Half Pyramid Pattern
// Java Program to print Pyramid pattern
import java.util.*;
public class Pattern {
// Function to demonstrate pattern
void printPattern(int n)
{
int i, j;
// outer loop to handle rows
for (i = 1; i <= n; i++) {
// inner loop to handle columns
for (j = 1; j <= i; j++) {
System.out.print("*");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
System.out.println("Enter no. of rows");
Java Dr. Nabanita Das
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
Pattern obj=new Pattern();
obj.printPattern(n);
}
}
Enter no. of rows
5
*
**
***
****
*****