|
| 1 | +--- |
| 2 | +title: Print an Integer |
| 3 | +description: In this program, you'll learn to print a predefined number aw well as a nummber entered by the user in Java. |
| 4 | +--- |
| 5 | + |
| 6 | +import { Heading } from '@/components/Heading' |
| 7 | +import { List, ListItemGood } from '@/components/List' |
| 8 | +import Link from 'next/link' |
| 9 | + |
| 10 | +The integer is stored in a variable using `System.in`, and is displayed on the screen using `System.out`. |
| 11 | + |
| 12 | +To understand this example, you should have the knowledge of the following Java programming topics: |
| 13 | + |
| 14 | +- [Java Hello World Program](/docs/hello-world) |
| 15 | +- Java Basic Input and Output |
| 16 | + |
| 17 | +## 1. Print an Integer |
| 18 | + |
| 19 | +A Java program that prints a number predefined by the user is as follows: |
| 20 | + |
| 21 | +### Example: How to Print an Integer |
| 22 | + |
| 23 | +#### Input |
| 24 | + |
| 25 | +```java |
| 26 | +public class HelloWorld { |
| 27 | + |
| 28 | + public static void main(String[] args) { |
| 29 | + |
| 30 | + // assing a variable to store the integer |
| 31 | + int number = 10; |
| 32 | + |
| 33 | + // println() prints the following line to the output screen |
| 34 | + System.out.println("The number is: " + number); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +``` |
| 39 | + |
| 40 | +#### Output |
| 41 | + |
| 42 | +```text n |
| 43 | +The number is: 10 |
| 44 | +``` |
| 45 | +In this program we are printing a constant integer value which is definrd from first. |
| 46 | +The integer is stored in an integer variable `number` using the keyword `int` and printed using the keyword `println`. |
| 47 | + |
| 48 | +## 2. Print an Integer (Entered by the User) |
| 49 | + |
| 50 | +A Java program that prints a number entered by the user is as follows: |
| 51 | + |
| 52 | +### Example: How to Print an Integer entered by an user |
| 53 | + |
| 54 | +#### Input |
| 55 | + |
| 56 | +```java |
| 57 | +import java.util.Scanner; |
| 58 | + |
| 59 | +public class HelloWorld { |
| 60 | + |
| 61 | + public static void main(String[] args) { |
| 62 | + |
| 63 | + // Creates a reader instance which takes |
| 64 | + // input from standard input - keyboard |
| 65 | + Scanner reader = new Scanner(System.in); |
| 66 | + System.out.print("Enter a number: "); |
| 67 | + |
| 68 | + // nextInt() reads the next integer from the keyboard |
| 69 | + int number = reader.nextInt(); |
| 70 | + |
| 71 | + // println() prints the following line to the output screen |
| 72 | + System.out.println("You entered: " + number); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +``` |
| 77 | + |
| 78 | +#### Output |
| 79 | + |
| 80 | +```text n |
| 81 | +Enter a number: 10 |
| 82 | +You entered: 10 |
| 83 | +``` |
| 84 | + |
| 85 | +In this program, an object of `Scanner` class, `reader` is created to take inputs from standard input, which is `keyboard`. |
| 86 | + |
| 87 | +Then, `Enter a number` prompt is printed to give the user a visual cue as to what they should do next. |
| 88 | + |
| 89 | +`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`. |
| 90 | + |
| 91 | +If you enter any character which is not an integer, the compiler will throw an `InputMismatchException`. |
| 92 | + |
| 93 | +Finally, `number` is printed onto the standard output `(System.out)` - computer screen using the function `println()`. |
0 commit comments