Java Program to Print And Read An Integer From User Input

 

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



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


Java is a versatile and widely-used programming language known for its portability, robustness, and simplicity. One of the fundamental tasks when learning any programming language is understanding how to take input from the user and display it back to them. In this article, we will explore how to write a Java program that prompts the user to enter an integer and then prints that integer.


Overview

User input in Java can be handled using various classes, but one of the most common and straightforward ways is through the Scanner class, which is part of the java.util package. This class provides methods to read different types of input, such as integers, strings, and doubles, from various input sources like the keyboard.


Step-by-Step Implementation

In the below program, the syntax and procedures to take the integer as input from the user are shown in Java language.


Steps for Input

  1. The user enters an integer value when asked.
  2. This value is taken from the user with the help of nextInt() method of Scanner Class. The nextInt() method, in Java, reads the next integer value from the console into the specified variable.

Coding Steps

  • Import the Scanner Class: The first step is to import the Scanner class from the java.util package.
  • Create a Scanner Object: Instantiate a Scanner object to read input from the standard input stream (keyboard).
  • Prompt the User for Input: Display a message to the user asking them to enter an integer.
  • Read the User Input: Use the appropriate method of the Scanner class to read the integer input from the user.

variableOfIntType = ScannerObject.nextInt();


where variableOfIntType is the variable in which the input value is to be stored.
And ScannerObject is the beforehand created object of the Scanner class.


Steps for Output

  1. This entered value is now stored in the variableOfIntType.
  1. Now to print this value, System.out.println() or System.out.print() method is used. The System.out.println() method, in Java, prints the value passed as the parameter to it, on the console screen and the changes the cursor to the next line on the console. Whereas System.out.print() method, in Java, prints the value passed as the parameter to it, on the console screen and the cursor remains on the next character of the last printed character on the console.

Syntax of println


System.out.println(variableOfXType);


Program Code




Explanation of the Code


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.

reader.nextInt() then reads all entered integers from the keyboard unless it encounters a new line character \n (Enter). The entered integers are then saved to the integer variable number.

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

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

Running the Program

To run this program, you need a Java development environment set up on your computer. Save the code in a file named PrintInteger.java, then compile and run it using the following commands:

javac PrintInteger.java
java PrintInteger

You will be prompted to enter an integer, and the program will print the integer you entered.


Conclusion

In this article, we demonstrated how to write a simple Java program to read an integer from the user and print it. This basic example covers fundamental concepts such as importing classes, creating objects, and handling user input, which are essential skills for any Java programmer. With this knowledge, you can start building more complex programs that interact with users and process various types of input.

Post a Comment

Post a Comment (0)

Previous Post Next Post