The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to read input from console in java using scanner

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
How to read input from console in java using scanner Posted: Aug 19, 2016 12:14 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: How to read input from console in java using scanner
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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:

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

Read: How to read input from console in java using scanner

Topic: Uber’s First Self-Driving Fleet Arrives in Pittsburgh This Month Previous Topic   Next Topic Topic: Java Tutorial : Java IO (Scanner class - Read file data)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use