Java Program to Print an Double (Entered by the User)

Basic Java programming exercises

Java Tutorial Java Exercises Data Structures

In this program, you'll learn to print a number entered by the user in Java. The Double is stored in a variable using System.in, and is displayed on the screen using System.out.







import java.util.Scanner; class Test { public static void main(String[] args) { Double number; Scanner op=new Scanner(System.in); System.out.print("Enter any Double number:"); number=op.nextDouble(); System.out.println("Display Double Number:"+number); } }




Output:

Enter any Double number:31.36 Display Double Number:31.36

In this program, an object of Scanner class, reader is created to take inputs from standard input, which is keyboard.

Then, Enter a number prompt is printed to give the user a visual cue as to what they should do next.

op.nextFloat() then reads all entered double from the keyboard unless it encounters a new line character \n (Enter). The entered floats are then saved to the double variable number.

If you enter any character which is not an double, the compiler will throw an InputMismatchException.

Finally, number is printed onto the standard output (System.out) - computer screen using the function println().










Instagram