Basics – Scanner, System.in, System.out.println()

SCANNER CLASS:

Scanner class is used to get input from user.

  • Scanner is a predefined class in java
  • Scanner is a final class. We can’t inherited Scanner class.

Import the scanner class:

  • Scanner class presented in java.util package.
  • We want to import the package, before using the scanner class.
import java.util.Scanner;

Create Scanner Object:

Scanner sc = new Scanner(System.in);
  • Here, Scanner is class.
  • sc is an object of Scanner class

System.in:

Scanner(System.in);
  • Above line is the Scanner Constructor.
  • Passing System.in as a parameter.

SYSTEM CLASS:

  • System is a predefined class in java.
  • System class Presented in java.lang package
    • java.lang package is a default package in Java.
    • Therefore, there is no need to import it explicitly.
  • System class is a final class. So, we can’t inherited this class.
  • System class can’t be instantiated. i.e. Can’t create objects for system class.
  • Because System class have private constructor.
  • System class properties are static in nature. so we can call those properties by class name.
  • The System class supports following class.
    • Java Predefined Class for Standard Input – InputStream Class
    • Java Predefined Class for Standard Output – PrintStream Class

InputStream Class:

  • InputStream is a predefined class in java
  • It is presented in java.io package
  • This is abstract class.
    • We can’t create object for abstract class.
    • We can create object reference for abstract class

PrintStream Class:

  • PrintputStream is a predefined class in java.
  • It is presented in java.io package

System.in: [Standard input]

  • System –> Predefined class
  • in
    • in -> static member of system class
    • in -> object of InputStream class

System.out.println( ): [Standard output]

  • System –> Predefined class in java.lang package
  • out
    • out -> Static member of the System class.
    • out -> Object of PrintStream Class
  • println() -> PrintStream method

Scanner Methods:

  • Scanner class have many methods to read the input of different datatype from user.
  • We can access those methods by Scanner class object.

Ex 1:

import java.util.Scanner;

public class Demo1 
{
	public static void main(String[] args) 
	{

		// creates an object of Scanner
		// System.in --> takes input from the keyboard

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter your name: ");

		// Scanner method calling based on input data type
		
		String name = sc.nextLine();

		// prints the input
		
		System.out.println("Welcome Ms." + name);
	}
}

Output:


Ex 2:

import java.util.Scanner;

public class Demo1 {
	public static void main(String[] args) 
	{

		// creates a Scanner object
		// System.in --> takes input from the keyboard
		
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter an integer: ");

		// Scanner method calling based on input data type
		
		int data1 = sc.nextInt();

		System.out.println("Using nextInt(): " + data1);
	}
}

Output:


Ex 3:

import java.util.Scanner;

class Demo1 {
  public static void main(String[] args) {

    // creates an object of Scanner
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter your name: ");

    //reads the word
    String value1 = sc.next();

    System.out.println("Using next(): " + value1);

  }
}

Output:


NOTE:

  • nextLine( ) —> Print the line
  • next( ) —> Print the first word. Divide the line by white space

Leave a comment

Design a site like this with WordPress.com
Get started