Using Scanner for reading data from system console in Java

Java has one more way for reading data from system console. Scanner was introduced in Java 1.5. Scanner class allows to read data from system console. Here I want to show example about it.
Example reads String and int values and then prints them:

import java.util.Scanner;public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.print("Username:"); String username = scanner.nextLine(); System.out.print("Number:"); int number = scanner.nextInt(); System.out.println("Username is:" + username); System.out.println("Number is:" + number); }}

 

Download code from this post.

 

Want to have latest information from this site? Subscribe to our feed.

Share

No related posts.

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.