Print integer java – Java Program to Print the Integer entered by User

Print integer java: Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Printing the integer entered by User.

Java print integer: In this article we will see different ways to print an integer which is entered by the user.

User can take input in multiple ways in java like by using

  1. Scanner class
  2. Buffered Reader class
  3. Console class

In this article we will not focus on how to read user input rather we will see how we can print an integer user input.

print() and println() :

How to print an integer in java: Both print() method and println() method in java belongs to PrintStream class. The difference between them is very minimal. Actually both methods display output text on the console. But in print() method the cursor remains at the end of text at the console while in println() method after printing the output text in console the cursor comes to next line at the console. This is the basic difference between two.

Printing the integer by using print() method :

Below code is the implementation to see how we can print an user entered integer.

/ util package imported
import java.util.*;

public class Main 
{

    public static void main(String[] args) 
    {

        // Scanner class object created to take input from user
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter an integer: ");

        // Here nextInt() reads integer input from key board
        int digit = sc.nextInt();

        // print() prints the integer inputed by the user
        // After printing cursor remains at the last of the output text at the console
        // Cursore do not go to next line
        System.out.print("Printing user entered input:" + digit);
    }
}
Output :
Please enter an integer: 5
Printing user entered input: 5

Printing the integer by using println() method :

Below code is the implementation to see how we can print an user entered integer.

// util package imported
import java.util.*;

public class Main 
{

    public static void main(String[] args) 
    {

        // Scanner class object created to take input from user
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter an integer: ");

        // Here nextInt() reads integer input from key board
        int digit = sc.nextInt();

        // println() prints the integer inputed by the user
        // After printing cursor goes to next line at the console
        System.out.println("Printing user entered input:" + digit);
    }
}
Output :
Please enter an integer: 5
Printing user entered input: 5
|

In this user entered integer printed but after printing the cursor moved to next line.

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Basic Programs: