- We are having special classes to take input from the user. One of the most commonly used class is java.util.Scanner.
- We can read input from the console using scanner class.
- Java.util.Scanner class provides lot of methods to take different typed of data from the user as input.
- Scanner class is final class in java like String class.
- Scanner class implements Iterator and Closeable interfaces.
Scanner class in Java
- public final class Scanner
- extends Object
- implements Iterator<String>, Closeable
- In order to read or take input from user we need to create object of Scanner class by using its constructor which takes System.in as an argument.
- By using methods of System class we can take different type of data like string , int and float etc.
- scannerObject.nextInt() will accepts integer value as an input .
- scannerObject.nextLong() will accepts long value as an input from user.
- We also take different type of data by using some delimiters
- Using useDelimiter() method we can give data by using delimiters.
- Lets see what are the different methods in java.util.scanner class.
Java.util.Scanner class methods:
Scanner class in java programming:
- Lets see some simple java programs to get input from user
- How to take integer input from user in java?
- How to take input from user in java using scanner ?
- How to take string input in java using scanner class?
Program #1: Write a java example program to read integer type of data as an input from the user from console using scanner class
- package com.scannerjava;
- import java.util.Scanner;
- public class TakeInputUsingScanner {
- /**
- * @wesite:www.instanceofJava.com
- * @category: Java program to take input from user using scanner
- */
- public static void main(String[] args) {
- Scanner sc= new Scanner(System.in);
- System.out.println("Please enter any integer value as input");
- int x=sc.nextInt();
- System.out.println("Entered integer value is "+x);
- }
- }
Output:
- Please enter any integer value as input
- 33
- Entered integer value is 33
Program #2: Write a java example program to read String type of data as an input from the user from console using scanner class
- package com.scannerjava;
- import java.util.Scanner;
- public class TakeInputUsingScanner {
- /**
- * @wesite:www.instanceofJava.com
- * @category: Java program to take input from user using scanner
- */
- public static void main(String[] args) {
- Scanner sc= new Scanner(System.in);
- System.out.println("Please enter any Sting value as input");
- String str=sc.next();
- System.out.println("Entered String value is "+str);
- }
- }
Output:
- Please enter any String value as input
- instanceofjava
- Entered String value is instanceofjava
Program #3: Write a java example program to read data as an input from the user from console using scanner class with delimiters
- package com.scannerjava;
- import java.util.Scanner;
- public class TakeInputUsingScanner {
- /**
- * @wesite:www.instanceofJava.com
- * @category: Java program to take input from user using scanner
- */
- public static void main(String[] args) {
- String input = "top 10 java interview questions on scanner class";
- Scanner sc = new Scanner(input).useDelimiter("\\s");
- System.out.println(sc.next());
- System.out.println(sc.nextInt());
- System.out.println(sc.next());
- System.out.println(sc.next());
- System.out.println(sc.next());
- System.out.println(sc.next());
- System.out.println(sc.next());
- System.out.println(sc.next());
- }
- }
Output:
- top
- 10
- java
- interview
- questions
- on
- scanner
- class
Program #4: Write a java example program to read all type of data as an input from the user from console using scanner class
- package com.scannerjava;
- import java.util.Scanner;
- public class TakeInputUsingScanner {
- /**
- * @wesite:www.instanceofJava.com
- * @category: Java program to take input from user using scanner
- */
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter an integer value");
- System.out.println(sc.nextInt());
- System.out.println("Enter a float value");
- System.out.println(sc.nextFloat());
- System.out.println("Enter a Double value");
- System.out.println(sc.nextDouble());
- System.out.println("Enter a Byte value");
- System.out.println(sc.nextByte());
- System.out.println("Enter a Long value");
- System.out.println(sc.nextLong());
- }
- }
Output:
- Enter an integer value
- 12
- 12
- Enter a float value
- 12.34
- 12.34
- Enter a Double value
- 12.3456789
- 12.3456789
- Enter a Byte value
- 2
- 2
- Enter a Long value
- 9876636363536
- 9876636363536
Program #5: Write a java example program how to read input from console in java eclipse all type of data as an input from the user from console using scanner class.
No comments