Day 12 : Scanner Class in Java

Day 12 : Scanner Class in Java

Scanner Class :

  • Scanner class in Java is found in the java.util package. Java provides various ways to read input from the keyboard, the java.util.Scanner class is one of them.
  • The Java Scanner class breaks the input into tokens using a delimiter which is whitespace by default. It provides many methods to read and parse various primitive values.
  • The Java Scanner class is widely used to parse text for strings and primitive types using a regular expression. It is the simplest way to get input in Java. By the help of Scanner in Java, we can get input from the user in primitive types such as int, long, double, byte, float, short, etc.
  • The Java Scanner class extends Object class and implements Iterator and Closeable interfaces.
  • The Java Scanner class provides next() methods to return the type of value such as nextInt(), nextByte(), nextShort(), next(), nextLine(), nextDouble(), nextFloat(), nextBoolean(), etc. To get a single character from the scanner, you can call next().charAt(0) method which returns a single character.

Input Type Methods :

  • nextInt() : Reads a int value from the user
  • nextLine() : Reads a String value from the user
  • nextBoolean() : Reads a boolean value from the user
  • nextByte() : Reads a byte value from the user
  • nextFloat() : Reads a float value from the user
  • nextDouble() : Reads a double value from the user
  • nextLong() : Reads a long value from the user
  • nextShort() : Reads a short value from the user

Example :

// Read a Line of Text Using Scanner
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Enter your name : ");
    String name = s.nextLine();
    System.out.println("My name is " + name);
    s.close();
  }
}        
Output :
Enter your name : Poojaa
My name is Poojaa        

In this example :

  • Scanner s = new Scanner(System.in); : Here, we have created an object of Scanner named input.
  • The System.in parameter is used to take input from the standard input. It works just like taking inputs from the keyboard.
  • We have then used the nextLine() method of the Scanner class to read a line of text from the user.

Example :

// Java Scanner nextInt()
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Enter an integer : ");
    int value = s.nextInt();
    System.out.println("The entered integer value is " + value);
    s.close();
  }
}        
Output :
Enter an integer : 10
The entered integer value is 10        

In this example,

  • We have used the nextInt() method to read an integer value.

Example :

// Java Scanner nextDouble()
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Enter Double value : ");
    double value = s.nextDouble();
    System.out.println("The entered double value is " + value);
    s.close();
  }
}        
Output :
Enter Double value : 11.11
The entered double value is 11.11        

In this example,

  • We have used the nextDouble() method to read a floating-point value.

Example :

// Java Scanner nextLine()
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Enter your name : ");
    String value = s.nextLine();
    System.out.println("My name is " + value);
    s.close();
  }
}        
Output :
Enter your name : Poojaa
My name is Poojaa        

In this example :

  • We have used the nextLine() method to read a string from the user.
  • The nextLine() method reads the entire line of input including spaces. The method is terminated when it encounters a next line character, \n.

Example :

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Enter your name : ");
    String name = s.nextLine();
    System.out.print("Enter your age : ");
    int age = s.nextInt();
    System.out.println("Name : " + name);
    System.out.println("Age : " + age);
  }
}        
Output :
Enter your name : Poojaa
Enter your age : 22
Name : Poojaa
Age : 22        

In this example :

  • Used nextLine() method to read a name string from the user.
  • Used nextInt() method to read a age integer from the user.
  • Prints Name and Age.


要查看或添加评论,请登录

POOJAASHREE P V的更多文章

  • Day 13 : File Handling in Java

    Day 13 : File Handling in Java

    File Class : The File class of the java.io package is used to perform various operations on files and directories.

  • Day 11 : Exception Handling in Java

    Day 11 : Exception Handling in Java

    Exception : An exception is an event that disrupts the normal flow of the program. It is an object which is thrown at…

  • Day 10 : Arrays in Java

    Day 10 : Arrays in Java

    Arrays : Array is an object which contains elements of a similar data type. The elements of an array are stored in a…

  • Day 9 : Date and Time in Java

    Day 9 : Date and Time in Java

    Date and Time : In Java, managing date and time involves several classes from the package, introduced in Java 8. The…

  • Day 8 : String, String Methods, String Builder and String Buffer in Java

    Day 8 : String, String Methods, String Builder and String Buffer in Java

    String : String is an object that represents a sequence of characters. The java.

  • Day 7 : Math Class and Math Methods in Java

    Day 7 : Math Class and Math Methods in Java

    Math Class : Math class provides several methods to work on math calculations like min(), max(), avg(), sin(), cos()…

  • Day 6 : Methods in Java

    Day 6 : Methods in Java

    Method : A method is a block of code or collection of statements or a set of code grouped together to perform a certain…

  • Day 5 : Looping Statements in Java

    Day 5 : Looping Statements in Java

    Looping Statements : Looping statements are used to execute a block of code repeatedly based on certain conditions…

    1 条评论
  • Day 4 : Conditional Statements in Java

    Day 4 : Conditional Statements in Java

    Conditional Statements : Conditional statements are control flow statements that allow you to execute different blocks…

  • Day 3 : Variables, Data Types and Operators

    Day 3 : Variables, Data Types and Operators

    Variables : A variable is a container which holds the value while the Java program is executed. A variable is assigned…

社区洞察

其他会员也浏览了