blog posts

Java Input And Output Tutorials

Input and Output Tutorial in Java (In Very Simple Language). In this tutorial, you will learn simple ways to display output and get input from the user.

Java output

It can be from

System.out.println ()

Or System.out.print ()

System.out.printf ()

Used to send output to standard output (screen).

System is a class, and out is a general static field that takes the output data. Do not worry if you do not notice. The following chapters will explain the classes, public, and static.

Let’s take an example of printing a sentence as an output.

  1. class AssignmentOperator {
  2. public static void main (String [] args) {
  3. System.out.println (“Java programming is interesting.”);
  4. }
  5. }

Output

Java programming is interesting.

Here, println is the method that displays the String inside the quotes.

What is it? The difference between println (), print (), and printf ()

Print () – Prints the String inside the quote.

Println () – Prints the String inside the quote. The cursor then moves to the beginning of the following line.

Printf () – Provides String formatting (similar to printf in C++ / C programming).

Example 2: print () and println ()

  1. class Output {
  2. public static void main (String [] args) {
  3. System.out.println (“1. println”);
  4. System.out.println (“2. println”);
  5. System.out.print (“1. print”);
  6. System.out.print (“2. print”);
  7. }
  8. }

Output

1. println

۲. println

1. print 2. print

Do not use quotation marks to display integers, variables, etc.

Example 3: Printing variables and literals

  1. class Variables {
  2. public static void main (String [] args) {
  3. Double number = -10.6;
  4. System.out.println (5);
  5. System.out.println (number);
  6. }
  7. }

Output

5

10.6

You can use the + operator to join strings and print them.

Example 4: Print interconnected strings

  1. class PrintVariables {
  2. public static void main (String [] args) {
  3. Double number = -10.6;
  4. System.out.println (“I am” + “awesome.”);
  5. System.out.println (“Number =” + number);
  6. }
  7. }

Output

I am awesome.

Number = -10.6

Consider the following line:

System.out.println (“I am” + “awesome.”);

The “I am” and “awesome” strings are appended to the page before printing.

In the following line:

System.out.println (“Number =” + number);

First, the compiler evaluates the value of the number variable and converts it to a string. Then, the strings are collected and printed on the screen.

Java input

There are several ways to get user input in Java. Here, you will learn how to use the Scanner object.

To do this, you need to import the Scanner class into the program:

import java.util.Scanner;

Then, we create an object of the Scanner class that is used to receive input from the user.

Scanner input = new Scanner (System.in);

int number = input.nextInt ();

Example 5: Get the input integer from the user

  1. import java.util.Scanner;
  2. class Input {
  3. public static void main (String [] args) {
  4. Scanner input = new Scanner (System.in);
  5. System.out.print (“Enter an integer:”);
  6. int number = input.nextInt ();
  7. System.out.println (“You entered” + number);
  8. }
  9. }

Output

Enter an integer: 23

You entered 23

Here, the input object of the Scanner class is created. The nextInt () method in the Scanner class then receives the integer input from the user.

To get long, float, double, and String input from the user, you can use the methods, respectively

nextLong (), nextFloat (), nextDouble (), and next ()

use.

Example 6: Get float, double, and String inputsJava

  1. import java.util.Scanner;
  2. class Input {
  3. public static void main (String [] args) {
  4. Scanner input = new Scanner (System.in);
  5. // Getting float input
  6. System.out.print (“Enter float:”);
  7. float myFloat = input.nextFloat ();
  8. System.out.println (“Float entered =” + myFloat);
  9. // Getting double input
  10. System.out.print (“Enter double:”);
  11. double myDouble = input.nextDouble ();
  12. System.out.println (“Double entered =” + myDouble);
  13. // Getting String input
  14. System.out.print (“Enter text:”);
  15. String myString = input.next ();
  16. System.out.println (“Text entered =” + myString);
  17. }
  18. }

Output

Enter float: 2,343

Float entered = 2,343

Enter double: -23.4

Double entered = -23.4

Enter text: Hey!

Text entered = Hey!