» » » How to take input from user in java using scanner

  • 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

  1. public final class Scanner
  2. extends Object
  3. 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 user input.png


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

  1. package com.scannerjava;

  2. import java.util.Scanner;

  3. public class TakeInputUsingScanner {

  4. /**
  5.    * @wesite:www.instanceofJava.com
  6.    * @category: Java program to take input from user using scanner
  7.    */
  8. public static void main(String[] args) {
  9.     Scanner sc= new Scanner(System.in);
  10.      System.out.println("Please enter any integer value as input");
  11.       int x=sc.nextInt();
  12.       System.out.println("Entered integer value is "+x);

  13. }
  14. }

 Output:

  1. Please enter any integer value as input
  2. 33
  3. 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


  1. package com.scannerjava;

  2. import java.util.Scanner;

  3. public class TakeInputUsingScanner {

  4. /**
  5.    * @wesite:www.instanceofJava.com
  6.    * @category: Java program to take input from user using scanner
  7.    */
  8. public static void main(String[] args) {
  9.     Scanner sc= new Scanner(System.in);
  10.      System.out.println("Please enter any Sting value as input");
  11.      String str=sc.next();
  12.       System.out.println("Entered String value is "+str);

  13. }
  14. }


 Output:

  1. Please enter any String value as input
  2. instanceofjava
  3. 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


  1. package com.scannerjava;

  2. import java.util.Scanner;

  3. public class TakeInputUsingScanner {

  4. /**
  5.    * @wesite:www.instanceofJava.com
  6.    * @category: Java program to take input from user using scanner
  7.    */
  8. public static void main(String[] args) {
  9.  
  10. String input = "top 10 java interview questions on scanner class";
  11.  Scanner sc = new Scanner(input).useDelimiter("\\s");  
  12.  
  13.      System.out.println(sc.next());  
  14.      System.out.println(sc.nextInt());  
  15.      System.out.println(sc.next());  
  16.      System.out.println(sc.next());  
  17.      System.out.println(sc.next());  
  18.      System.out.println(sc.next());  
  19.      System.out.println(sc.next());    
  20.       System.out.println(sc.next()); 
  21. }
  22. }


 Output:

  1. top
  2. 10
  3. java
  4. interview
  5. questions
  6. on
  7. scanner
  8. 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

  1. package com.scannerjava;

  2. import java.util.Scanner;

  3. public class TakeInputUsingScanner {

  4. /**
  5.    * @wesite:www.instanceofJava.com
  6.    * @category: Java program to take input from user using scanner
  7.    */
  8. public static void main(String[] args) {
  9.  
  10. Scanner sc = new Scanner(System.in);
  11.  
  12.  System.out.println("Enter an integer value");
  13. System.out.println(sc.nextInt()); 
  14.  
  15. System.out.println("Enter a float value");
  16.  System.out.println(sc.nextFloat()); 
  17.  
  18.  System.out.println("Enter a Double value");
  19. System.out.println(sc.nextDouble()); 
  20.  
  21. System.out.println("Enter a Byte value");
  22. System.out.println(sc.nextByte()); 
  23.  
  24. System.out.println("Enter a Long value");
  25. System.out.println(sc.nextLong());
  26. }
  27. }


 Output:

  1. Enter an integer value
  2. 12
  3. 12
  4. Enter a float value
  5. 12.34
  6. 12.34
  7. Enter a Double value
  8. 12.3456789
  9. 12.3456789
  10. Enter a Byte value
  11. 2
  12. 2
  13. Enter a Long value
  14. 9876636363536
  15. 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.

java util scanner

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu