Input And Output Tutorial In Java (In Very Simple Language)
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
Can simply 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. We will explain the classes, public and static in the following chapters.
Let’s take an example to print a sentence as output.
- class AssignmentOperator {
- public static void main (String [] args) {
- System.out.println (“Java programming is interesting.”);
- }
- }
Output
Java programming is interesting.
Here, println is the method that displays the string inside the quote.
What is it? The difference between println () and print (), printf ()
Print () – Prints the string inside the quote.
Println () – Prints the string inside the quote. The cursor then moves to the beginning of the next line.
Printf () – Provides string formatting (similar to printf in C ++ / C programming).
Example 2: print () and println ()
- class Output {
- public static void main (String [] args) {
- System.out.println (“1. println”);
- System.out.println (“2. println”);
- System.out.print (“1. print”);
- System.out.print (“2. print”);
- }
- }
Output
1. println
۲. println
1. print 2. print
Do not use quotation marks to display integers, variables, and so on.
Example 3: Printing variables and literals
- class Variables {
- public static void main (String [] args) {
- Double number = -10.6;
- System.out.println (5);
- System.out.println (number);
- }
- }
Output
5
10.6
You can use the + operator to join strings and print them.
Example 4: Print interconnected strings
- class PrintVariables {
- public static void main (String [] args) {
- Double number = -10.6;
- System.out.println (“I am” + “awesome.”);
- System.out.println (“Number =” + number);
- }
- }
Output
I am awesome.
Number = -10.6
Consider the following line:
System.out.println (“I am” + “awesome.”);
“I am” and “awesome” strings. Before printing on the page, they are first appended.
In the following line:
System.out.println (“Number =” + number);
First, the value of the number variable is evaluated and converted to a string by the compiler. Then, the strings are collected and printed on the screen.
Java input
There are several ways to get input from a user 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
- import java.util.Scanner;
- class Input {
- public static void main (String [] args) {
- Scanner input = new Scanner (System.in);
- System.out.print (“Enter an integer:”);
- int number = input.nextInt ();
- System.out.println (“You entered” + number);
- }
- }
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 is then used to receive 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 inputs
- import java.util.Scanner;
- class Input {
- public static void main (String [] args) {
- Scanner input = new Scanner (System.in);
- // Getting float input
- System.out.print (“Enter float:”);
- float myFloat = input.nextFloat ();
- System.out.println (“Float entered =” + myFloat);
- // Getting double input
- System.out.print (“Enter double:”);
- double myDouble = input.nextDouble ();
- System.out.println (“Double entered =” + myDouble);
- // Getting String input
- System.out.print (“Enter text:”);
- String myString = input.next ();
- System.out.println (“Text entered =” + myString);
- }
- }
Output
Enter float: 2,343
Float entered = 2,343
Enter double: -23.4
Double entered = -23.4
Enter text: Hey!
Text entered = Hey!