Java Program to Get Input from User

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

This Java 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);

	}
}
Java Program to Get Input from User

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

package RemainingSimplePrograms;

import java.util.Scanner;

public class UserInputs2 {
	
	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 = Java
User Entered String = Java

Please Enter any Character =  M
User Entered Character = M

Please Enter any Byte =  9
User Entered Byte = 9