Java Program to read and display input from user

Here we are explaining how to write a java program to read a different type of inputs from the user and how to display it.

How to read an integer number from the user in java?

In java, an integer number is read from the user by using nextLine() method of the java Scanner class. So first we have to create an object of the scanner class by passing System.in as the parameter. And also we have to import java.util.Scanner.An example is shown below.

    Scanner sc = new Scanner(System.in);

    n= sc.nextInt();

How to read a string from the user in java?

In java, a string is read from the user by using nextLine() method of the java Scanner class. An example is shown below.

    Scanner sc = new Scanner(System.in);

   str = sc.nextLine();

How to read a float number from the user in java?

In java, a float number is read from the user by using nextfloat() method of the java Scanner class. An example is shown below.

    Scanner sc = new Scanner(System.in);

     f= sc.nextFloat();

How to read a double number from the user in java?

In java, a double number is read from the user by using nextdouble() method of the java Scanner class. An example is shown below.

    Scanner sc = new Scanner(System.in);

   d= sc.nextDouble();

 

ALGORITHM

STEP 1: Declare the class UserInput with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the variables n as integer,f as float,d as double, str as String.

STEP 4: Create an object of the scanner class as sc.

STEP 5: Read a  string from the user using sc.nextLine() into the variable str.

STEP 6: Display the string as str by using System.out.println().

STEP 7: Read an integer number from user using sc.nextInt() into the variable n.

STEP 8: Display the number n by using System.out.println().

STEP 9: Read a float number from the user using sc.nextFloat() into the variable f.

STEP 10: Display the float number f by using System.out.println().

STEP 7: Read a double number from the user using sc.nextDouble() into the variable d.

STEP 8: Display the double number d by using System.out.println().

Java Source Code

                                          import java.util.Scanner;

public class UserInput{
  public static void main(String args[]){
     int n;
     float f;
     double d;
     String str;
 
     Scanner sc = new Scanner(System.in);
     
      //String
     System.out.println("Enter the string: ");
     str = sc.nextLine();
     System.out.println("The String is: "+str);
     
     //Integer
     System.out.println("Enter an integer number: ");
     n= sc.nextInt();
     System.out.println("The Integer number is : "+n);
    
     //Float
     System.out.println("Enter the float number: ");
     f= sc.nextFloat();
     System.out.println("The float number is: "+f); 
     
      //Double
     System.out.println("Enter the double number: ");
     d= sc.nextDouble();
     System.out.println("The double number is: "+d); 
  }
}
                                      

OUTPUT

Enter the string: STEM
The String is: STEM
Enter an integer number: 10
The Integer number is : 10
Enter the float number: 20.3
The float number is: 20.3
Enter the double number: 50.63245
The double number is: 50.63245