Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Input & Output


Input and Output are preferably the most important statements in a programming language.


So far, we have seen the Output command of Java(i.e. System.out.println(...)).


Example :



public class MyApplication
{
    public static void main(String[] args)
     {
        System.out.println("Hello World");
    }
}



And we are yet to look at the Input statement.


So, let us look at the Input statement. Then we will be exploring a few more ways to use the Output statement.


Java Input


So far, we have used variables to store values. But the variables were loaded with values.


Example :



public class MyApplication
{
    public static void main(String[] args)
     {
         int x = 9;
         System.out.println("The value of x is : "+x);
    }
}


Output :



  The value of x is : 9

But, what if you don't want the variable x to contain a fixed value like 9. Rather you want the user to enter any value of their choice.


And this is where the Input statements comes to picture.


Just like the Output statement is called System.out.println(). Same way the Input statement is called as System.in.


However, to use System.in we need the help of Scanner class.


The Scanner class can be found in the java.util package.


Now, let us understand System.in statement using Scanner class with the below example.


Example :



import java.util.Scanner;

public class MyApplication {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter any text : ");
        String x = scanner.nextLine();

        System.out.println("The text you have entered is : "+x);
    }
}


Output :



  Enter any text : Hello
  The text you have entered is : Hello

Now, if you see the Output. The first line says,


Enter any text : Hello

Now, let us go to the above code and see what happened?


In the first line, we have prepared the Scanner class,


Scanner scanner = new Scanner(System.in);

In the next, we have used the print statement so that the user can input the values,


System.out.print("Enter any text : ")

Then we have declared a variable x, which is initially empty. And used the scanner.nextLine() to get the input from the user.


String x = scanner.nextLine();
Java-Input-&-Output


So, what happened is, the below String is Printed,


Enter any text :

Then we have the statement,


String x = scanner.nextLine();

nextLine() is used to get an input from the user, and if the user enters a value, it goes and sits inside variable x.


So, the screen is stuck, until user types any number.

Java-Input-&-Output

In the above case, I have typed Hello.


Enter any text : Hello

And Hello goes and sits inside x.

Java-Input-&-Output

And we got the output,


The text you have entered is : Hello

So, we have seen how can we get a String/Text as an input.


Now, let us say, you want to get a number as an input.


Get Number as Input


In that case, we can also use scanner.nextInt(), but with a minimal change.


Example :



public class MyApplication
{
    public static void main(String[] args) {
     
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter any text : ");
        int num = scanner.nextInt();

        System.out.println("The text you have entered is : "+num);
    }
}


Output :



  Enter a number of your choice : 21
  The number you have entered is : 21

Now, if you see the Output. The first line says,


Enter any number of your choice : 21

Now, let us go to the above code and see what happened?


In the above code, we have used the print statement,


	System.out.print("Enter a number of your choice : ")

Then we have declared a variable num, which is initially empty.


int num = scanner.nextInt();
Java-Input-&-Output


So, what happened is, the below String is Printed,


Enter a number of your choice :

Then we have the statement,


int num = scanner.nextInt();

nextInt() is used to get an input from the user, and if the user enters a value, it goes and sits inside variable x.


So, the screen is stuck, until user types any number.

Java-Input-&-Output

In the above case, I have typed 21.


Enter any text : 21

And 21 goes and sits inside x.

Java-Input-&-Output

Next, let us look at Java Output.


Java Output


A simple example of System.out.println(...) statement would be,


Example :



public class MyApplication
{
    public static void main(String[] args)
     {
         int x = 9;
         System.out.println("The value of x is : "+x);
    }
}	


Output :



  The value of x is : 9

String interpolation using '$'


With the System.out.println(...) statement, Java supports something called as String interpolation.


Let us dive into an example to understand String interpolation.


Example :



public class MyApplication
{
    public static void main(String[] args)
     {
        int x = 5;
	    int y = 8;
	    int z = x + y;
        System.out.println(String.format("The added result of %d and %d is %d", x, y, z));
    }
}


Output :



  The added result of 5 and 8 is 13

So, in the above example, we have three variables, x, y and z.


Now, inside the System.out.println(...) statement, we have used String.format().


System.out.println(String.format("The added result of %d and %d is %d", x, y, z));

Inside String.format(), we have used the placeholders as %d. %d takes an integer value.


So, %d expects an integer value.


And thus we have x, y and z inside String.format.


String.format("The added result of %d and %d is %d", x, y, z)

And what happens is, the values of variables x, y and z gets replaced with actual values i.e. 5, 8 and 13.


And we get the below output,

Output :



  The added result of 5 and 8 is 13