Skip to main content

Command Palette

Search for a command to run...

Java Input and Output

Java Input:

Updated
Java Input and Output
M

IIIT - N Software Geek. Startup Enthusiast. Build for the Future.

  • We have different options to take input from the user.

  • One of the key methods to get input from the user is using the Scanner Object Class.

  • To use the Scanner Object first we need to import the java.util.Scanner package.

    • import java.util.Scanner; with this, we can able to import the scanner Package.
  • Then we need to create the object concerning the Scanner class.

//creating an scanner object.
Scanner sc = new Scanner(System.in);

//take input from the user.
int number = sc.nextInt();

Java Output:

For Output purposes, we can simply use the

  • System.out.println(); // prints the item and move the cursor to the beginning of the next line.

  • System.out.print(); // prints the item

  • System.out.printf(); // it provides the formatting of the string.

Here System is a Class and Out is a public static field which accepts the output data.

Example Program :

//importing the java 
import java.util.Scanner;
public class Main{
    public static void main(String[] args){

        //creating the scanner object
        System.out.print("Enter a Number : ");
        Scanner sc = new Scanner(System.in);

        // taking the value from the user
        int number = sc.nextInt();


        // using java output 
        System.out.println(number + " Hello World");

    }
}

Output :

More from this blog