0% found this document useful (0 votes)
21 views3 pages

Scanner Class

The Scanner class in Java provides methods to read input from various sources like the keyboard. It has methods like nextInt() to read integers, nextDouble() to read doubles, and nextLine() to read strings. Examples show how to use the Scanner class to read user input like name, age, and numbers and perform operations on them. String comparisons are also demonstrated, showing the difference between == which compares references and .equals() which compares content.

Uploaded by

bikashsingh3994
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
21 views3 pages

Scanner Class

The Scanner class in Java provides methods to read input from various sources like the keyboard. It has methods like nextInt() to read integers, nextDouble() to read doubles, and nextLine() to read strings. Examples show how to use the Scanner class to read user input like name, age, and numbers and perform operations on them. String comparisons are also demonstrated, showing the difference between == which compares references and .equals() which compares content.

Uploaded by

bikashsingh3994
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

Scanner Class

Scanner Class

 Scanner class provides set of methods to read data from different sources. This class is
available in java.util package.

Methods of Scanner Class

Method Description

nextInt() Read integer values

nextFloat() Read float values

nextDouble() Read double values

next() Read one word length string

nextLine() Read more than word length string

nextBoolean() Read boolean values

nextLong() Read long values

Reading Data From Keyboard Using Scanner Class

Example#1

import java.util.Scanner;

class ReadData{
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
System.out.print("Enter Your Name: ");
String name = ob.nextLine();
System.out.print("Enter Your Address: ");
String address = ob.nextLine();
System.out.print("Enter Your Age: ");
int age = ob.nextInt();
System.out.print("Enter Your Height: ");
double height = ob.nextDouble();
System.out.print("Enter Your Mobile: ");
long mobileno = ob.nextLong();
System.out.println("Name = "+name);
System.out.println("Address = "+address);
System.out.println("Age = "+age);
System.out.println("Height = "+height);
System.out.println("Mobile No = "+mobileno);
}
}

Example#2

import java.util.Scanner;

class ReadData{
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
System.out.print("Enter Number1: ");
int num1 = ob.nextInt();
System.out.print("Enter Number2: ");
int num2 = ob.nextInt();

System.out.println("****Result****");
System.out.println("Addition = "+(num1+num2));
System.out.println("Substraction = "+(num1-num2));
System.out.println("Multiplication = "+(num1*num2));
System.out.println("Division = "+(num1/num2));
}
}

Example#3

class ReadData{
public static void main(String[] args) {
String s1 = "BBSR";
String s2 = "BBSR";
System.out.println(s1 == s2);
}
}
Example4

class ReadData{
public static void main(String[] args) {
String s1 = new String("BBSR");
String s2 = new String("BBSR");
System.out.println(s1 == s2); // reference comparison
System.out.println(s1.equals(s2)); // content comparison
}
}

You might also like