Useful methods and implementation under Scanner Class.

To get user input at runtime we have to use Scanner Class. It contains various inbuilt methods which can be used to make user interaction more smoother and error free.

Scanner Class

Please visit below link to get theoretical as well as practical knowledge of Scanner Class and its methods:

Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).



In this post, we have combined all these methods and tried to describe the same using different functions.

Note: Please do comment all section in the code except that one for which you are looking. For any help or assistance do write us in comments at the end of the page or Contact Us.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Pattern;


/**
 * 
 */

/**
 * @author ashok.kumar
 *
 */
public class UserInput {
	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException 
	{
		//Sample of Printing table of user defined number
		table();
				
		inputInteger(); // Takes input only in Integer format.
		inputDouble();  // Takes input in integer and decimal format.
		inputBoolean(); // Takes input only in TRUE and FALSE strings.
		
	//Sample of taking USER Input via next() method.
	 Scanner sc=new Scanner(System.in);
	 System.out.print("Enter String value(s) seperated by spaces: ");
	String itemName=sc.next();
	System.out.println(itemName);
	
	//Sample of taking USER Input via nextLine() method.
	 Scanner sc1=new Scanner(System.in);
	 System.out.print("Enter String values in form of sentence: ");
	String storeAddress=sc1.nextLine();
	System.out.println(storeAddress);
		
		// Using next() with ARRAYS.
		inputNext();
		
		// Using nextLine() with ARRAYS.
		inputNextLine();
	
		// User REGULAR Expression to Avoid UNWANTED DATA from end user at runtime.
		inputPattern();
		
		// Reding external file and extracting WORD COUNT.
		hasNextExample();
		
		// Reding external file and extracting LINE COUNT.
		hasNextLineExample();
	}
	
	public static void inputInteger()
	{
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter integer value: ");
		int temp=sc.nextInt();
		
		System.out.println(temp);
	}
	
	public static void inputDouble()
	{
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter decimal value: ");
		double temp=sc.nextDouble();
		
		System.out.println(temp);
	}
	
	public static void inputBoolean()
	{
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter Boolean value: ");
		boolean temp=sc.nextBoolean();
		
		System.out.println(temp);
	}
	public static void inputNext()
	{
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter array lenght:");
		int number=sc.nextInt();
		
		String[] temp=new String[number];
		System.out.println("Start Typing: ");
		for(int i=0;i<number;i++)
		{
			temp[i]=sc.next();
		}
		
		System.out.println("Output:");
		for (String string : temp) {
			System.out.println(string);
		}
	}

	public static void inputNextLine()
	{
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter array lenght:");
		int number=sc.nextInt();
		
		Scanner sc1=new Scanner(System.in);
		String[] temp=new String[number];
		System.out.println("Start Typing: ");
		for(int i=0;i<number;i++) { temp[i]=sc1.nextLine(); } System.out.println("Output:"); for (String string : temp) { System.out.println(string); } } public static void inputPattern() { Scanner sc=new Scanner(System.in); System.out.print("Pattern 1: only letters:>");
		String emailID=sc.next(Pattern.compile("[a-z]*"));  // Only allows letters in the range a-z
		System.out.println(emailID);
		
		Scanner sc1=new Scanner(System.in);
		System.out.print("Pattern 2: only letters ends with @:>");
		emailID=sc1.next(Pattern.compile("[a-z]*@"));  // Only allows letters start with the range a-z and ends with @.
		System.out.println(emailID);
		
		Scanner sc2=new Scanner(System.in);
		System.out.print("Pattern 3: letters@letters.com :>");
		emailID=sc2.next(Pattern.compile("[a-z]*@[a-z]*.com")); // Allows a valid email id in *@*.com format and only letters.
		System.out.println(emailID);
		
		Scanner sc3=new Scanner(System.in);
		System.out.print("Pattern 4: letters_numbers@letters.com :>");
		emailID=sc3.next(Pattern.compile("[a-z,0-9]*@[a-z]*.com")); // Allows a valid email id in *@*.com format and only letters.
		
		System.out.println(emailID);
	}
	
	public static void hasNextExample() throws FileNotFoundException
	{
				
		Scanner sc1=new Scanner(new File("src\\UserInput.java"));
		int wordCount=0;
		
		while(sc1.hasNext())
		{
			System.out.println(sc1.next());
			wordCount++;
		}
		
		System.out.println("Total Words: "+wordCount);
		
	}
	
	public static void hasNextLineExample() throws FileNotFoundException
	{
				
		Scanner sc1=new Scanner(new File("src\\UserInput.java"));
		int lineCount=0;
		
		while(sc1.hasNextLine())
		{
			System.out.println(sc1.nextLine());
			lineCount++;
		}
		
		System.out.println("Total Lines: "+lineCount);
		
	}
	
	public static void table()
	{
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter any number: ");
		int userInput=sc.nextInt();
		
		System.out.println("Table of "+userInput);
		for(int i=1;i<=10;i++)
		{
			System.out.println(userInput*i);
		}
	}
}

Related Links:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:


1464total visits,4visits today

Leave a Reply

Your email address will not be published. Required fields are marked *