Read user input in Java using Scanner

Java Scanner class can be to read the user input from console. This program teaches you code for reading user input from console using the scanner class.

Ads

Tutorials   
How to convert String Date to Timestamp in Java? Best Java Tutorial for Beginners Read user input in Java using Scanner Java Programming Examples Read file line by line in Java 8 What are the benefits of learning Java Programming? A brief history and future of Java programming language Importance of Java for Mobile Application Development How To Become A Good Software Developer? Learn Java Programming for beginners 5 Reasons Java is Still a Great Language to Boost Your Career Prospect as a Programmer How to learn Java with no programming experience? How to learn Java? What are Classes and Interfaces in Java? Converting a String to Integer in Java How to check if a string exists in a string in JavaScript? Installing JDK 8 on Windows 10 and configuring the settings Video Tutorial of reading file in Java 9 Invincible Reasons to learn Java New to Java programming Java Programming Tips, Articles and Notes How to download JDK 1.6? Encapsulation in Java Programming How to format number in Java? Generics Example Program in Java Java 9 Features Java 9 Tutorial, news and examples How to read content of JPEG file in Java? Image Processing Tutorials in Java Programming Language Introduction to Java Technology Java 8 Consumer Interface with forEach Loop Introduction to Generics in Java Java 8 consumer class(interface) example Oracle Launches Project Valhalla for Java How to read properties file in Java? How to add JDK 8 support in Eclipse? Java 8: Java 8 is officially released and it can be downloaded How to Invoke method using Reflection API of Java? First Lambda Expressions in Java Java 8 expected release date

Example of of reading user input in Java using Scanner

In this section we are explaining the use of Scanner class in Java which can be used to read user input from console. Our sample program asks user to enter name and then age, user input is read using the scanner class. Information entered by user is printed back on the console.

There are various ways to read user input in Java and Scanner class is one of them. Scanner class can be used to read user input from console. It provide methods to read data in String, int, byte, short, Long, Float and Double formats. You can use this class to read and parse data using the regular expression.

Constructor

Scanner class takes input stream as the constructor parameter as shown below:

Scanner  scannerObj=new  Scanner(System.in);

In this above code we are constructing object of Scanner class by passing System.in as input stream. Scanner class is used to read from input stream using regular expressions and it provides methods to read data into various formats.

Example of of reading user input in Java using Scanner

Following are the most used methods of Scanner class:

Method Description
public byte nextByte() Use this method to read byte value from the stream
public short nextShort() If you have to read the short value then use this method
public int nextInt() Function is used to read the int value
public long nextLong() For reading the long value using Scanner class
public float nextFloat() For reading the float value
 public double nextDouble() Used to read value into double variable
public char nextChar() Read a char
public boolean nextBoolean() For reading boolean value
public String nextLine() For reading whole line

So, above are the methods that you can use with the Scanner class object. Now let's make a simple program to reading user input from console.

package net.roseindia;

import java.util.Scanner;

public class ReadConsoleScanner {

	public static void main(String[] args) {

	Scanner scanner = new Scanner(System.in);
	System.out.println("Enter your Name:");
	String name = scanner.nextLine();
	System.out.println("Enter your age:");
	int age = scanner.nextInt();
	System.out.println("Welcome  " +name 
          +"!!! You are "+age+" old!!");

	}
}

Following is the sample output of the program:

Enter your Name:
Deepak
Enter your age:
35
Welcome  Deepak!!! You are 35 old!!

So, in this example we learned to read user input from console, we used Scanner class to read line and int value from the console.

Related Examples:

Advertisements

Share on Google+Share on Google+

Read user input in Java using Scanner

Posted on: June 1, 2017 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Read user input in Java using Scanner  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads