Java Program to Get Input from User

Write a Java program to get input from the user or read the given console input. We have a Scanner class in the util package, allowing us to take the console’s content. Before using this class, we must create an instance of that class and use the methods.

This program reads the user given integer, double, and float values using nextInt(), nextDouble(), and nextFloat() methods.

package RemainingSimplePrograms;

import java.util.Scanner;

public class UserInputs1 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter Integer Value = ");
		int x = sc.nextInt();
		System.out.println("User Entered Integer Value = " + x);
		
		System.out.print("\nPlease Enter Double Value = ");
		double y = sc.nextDouble();
		System.out.println("User Entered Double Value = " + y);
			
		System.out.print("\nPlease Enter Float Value = ");
		float z = sc.nextFloat();
		System.out.println("User Entered Float Value = " + z);

	}
}
Get Input from User

In this get input from User program, we used nextLine(), next().charAt(0), nextByte() to read string, character, and byte from console. Please refer other Java programs.

import java.util.Scanner;

public class Example2 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter Any String = ");
		String str = sc.nextLine();
		System.out.println("User Entered String = " + str);
			
		System.out.print("\nPlease Enter any Character =  ");
		char ch = sc.next().charAt(0);
		System.out.println("User Entered Character = " + ch);
		
		System.out.print("\nPlease Enter any Byte =  ");
		Byte b = sc.nextByte();
		System.out.println("User Entered Byte = " + b);
	}
}
Please Enter Any String = Hello
User Entered String = Hello

Please Enter any Character =  M
User Entered Character = M

Please Enter any Byte =  9
User Entered Byte = 9