Write a java program to print a number from the user

Write a java program to print a number from the user. In the Java program, print the value obtained from the user. This is the first step in learning,  how to print a number in the Java programming language. For beginners, these are the simple java program to print and how to run them in java. 

import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int number;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number ");
number = scan.nextInt();
System.out.print("Entered number " +number);
}
}

 

Output:

Enter the number  456

Entered number 456

Java program to print the number

Java program to print the number will create an idea of programming language with student. It will lead the learner to learn more programs in java.

How the system.out.print works and why it is needed

The print() method is employed. In Java, the System. out. println() function writes the value supplied as a parameter to it on the console screen and moves the cursor to the next line. After that, the entered integers are saved in the integer variable number.

The compiler will issue an InputMismatchException if you enter any character that is not an integer. Finally, the function println() is used to print the number on the standard output ( System. out ) – computer screen.


print(): In Java, the print() function is used to display text on the console. This method accepts this text as a parameter in the form of a String. This method outputs the text to the console while keeping the cursor at the conclusion of the text.

What is integer in java program

In Java, an int is a primitive data type, but an Integer is a Wrapper class. Because int is a basic data type, it has little flexibility.

Only the binary value of an integer may be stored in it. Integer variables, like any other reference type, store references to Integer objects.

What is scanner in java and why it is needed?

The scanner is a class in the Java util package that is used to get the input of primitive kinds such as int, double, and strings.

It’s the simplest way to read input in a Java program, but it’s not particularly efficient if you’re looking for an input method for situations where time is a factor, such as competitive programming.

It implies importing all of Java’s classes and interfaces. Make the utilities in the util package available for usage within the current class or interface.

This is a shortcut wildcard annotation for importing all classes included within a certain package. This will not import the classes from the java sub-packages.

What is package in java programming and its uses?

A package is a namespace that groups together a collection of related classes and interfaces. Because software built in the Java programming language might have hundreds or thousands of distinct classes, it makes it logical to group similar classes and interfaces into packages to keep things organized.

In Java, a package is used to bring together similar classes. Consider it a folder in a file system. Packages help us avoid name clashes and produce more maintainable code.