{"id":1154,"date":"2022-02-24T19:04:11","date_gmt":"2022-02-24T19:04:11","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1154"},"modified":"2022-02-24T19:04:11","modified_gmt":"2022-02-24T19:04:11","slug":"basic-input-and-output","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/basic-input-and-output\/","title":{"rendered":"Basic Input and Output"},"content":{"rendered":"\n<p>In this tutorial, you will learn simple ways to display output to users and take input from users in Java.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"output\">Java Output<\/h2>\n\n\n\n<p>In Java, you can simply use<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.println(); or\n\nSystem.out.print(); or\n\nSystem.out.printf();\n<\/code><\/pre>\n\n\n\n<p>to send output to standard output (screen).<\/p>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ul><li><code>System<\/code>&nbsp;is a class<\/li><li><code>out<\/code>&nbsp;is a&nbsp;<code>public<\/code>&nbsp;<code>static<\/code>&nbsp;field: it accepts output data.<\/li><\/ul>\n\n\n\n<p>Don&#8217;t worry if you don&#8217;t understand it. We will discuss&nbsp;<code>class<\/code>,&nbsp;<code>public<\/code>, and&nbsp;<code>static<\/code>&nbsp;in later chapters.<\/p>\n\n\n\n<p>Let&#8217;s take an example to output a line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class AssignmentOperator {\n    public static void main(String&#91;] args) {\n    \t\n        System.out.println(\"Java programming is interesting.\");   \n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Java programming is interesting.<\/samp><\/code><\/pre>\n\n\n\n<p>Here, we have used the&nbsp;<code>println()<\/code>&nbsp;method to display the string.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"difference-print-println-printf\">Difference between println(), print() and printf()<\/h3>\n\n\n\n<ul><li><code>print()<\/code>\u00a0&#8211; It prints string inside the quotes.<\/li><li><code>println()<\/code>\u00a0&#8211; It prints string inside the quotes similar like\u00a0<code>print()<\/code>\u00a0method. Then the cursor moves to the beginning of the next line.<\/li><li><code>printf()<\/code>\u00a0&#8211; It provides string formatting (similar to\u00a0printf in C\/C++ programming).<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example: print() and println()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Output {\n    public static void main(String&#91;] args) {\n    \t\n        System.out.println(\"1. println \");\n        System.out.println(\"2. println \");\n    \t\n        System.out.print(\"1. print \");\n        System.out.print(\"2. print\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>1. println \n2. println <\/samp>\n<samp>\n1. print <\/samp>\n<samp>2. print\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have shown the working of the\u00a0<code>print()<\/code>\u00a0and\u00a0<code>println()<\/code>\u00a0methods. To learn about the\u00a0<code>printf()<\/code>\u00a0method,<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"print-variables\">Example: Printing Variables and Literals<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Variables {\n    public static void main(String&#91;] args) {\n    \t\n        Double number = -10.6;\n    \t\n        System.out.println(5);\n        System.out.println(number);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>When you run the program, the output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>5\n-10.6<\/samp><\/code><\/pre>\n\n\n\n<p>Here, you can see that we have not used the quotation marks. It is because to display integers, variables and so on, we don&#8217;t use quotation marks.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"print-strings\">Example: Print Concatenated Strings<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class PrintVariables {\n    public static void main(String&#91;] args) {\n    \t\n        Double number = -10.6;\n    \t\n        System.out.println(\"I am \" + \"awesome.\");\n        System.out.println(\"Number = \" + number);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>I am awesome.\nNumber = -10.6\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, notice the line,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.println(\"I am \" + \"awesome.\");\n<\/code><\/pre>\n\n\n\n<p>Here, we have used the&nbsp;<code>+<\/code>&nbsp;operator to concatenate (join) the two strings:&nbsp;<var>&#8220;I am &#8220;<\/var>&nbsp;and&nbsp;<var>&#8220;awesome.&#8221;<\/var>.<\/p>\n\n\n\n<p>And also, the line,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.println(\"Number = \" + number);\n<\/code><\/pre>\n\n\n\n<p>Here, first the value of variable&nbsp;<var>number<\/var>&nbsp;is evaluated. Then, the value is concatenated to the string:&nbsp;<var>&#8220;Number = &#8220;<\/var>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"input\">Java Input<\/h2>\n\n\n\n<p>Java provides different ways to get input from the user. However, in this tutorial, you will learn to get input from user using the object of&nbsp;<code>Scanner<\/code>&nbsp;class.<\/p>\n\n\n\n<p>In order to use the object of&nbsp;<code>Scanner<\/code>, we need to import&nbsp;<code>java.util.Scanner<\/code>&nbsp;package.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport java.util.Scanner;\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Then, we need to create an object of the&nbsp;<code>Scanner<\/code>&nbsp;class. We can use the object to take input from the user.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ create an object of Scanner\nScanner input = new Scanner(System.in);\n\n\/\/ take input from the user\nint number = input.nextInt();\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"integer-input\">Example: Get Integer Input From the User<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\nclass Input {\n    public static void main(String&#91;] args) {\n    \t\n        Scanner input = new Scanner(System.in);\n    \t\n        System.out.print(\"Enter an integer: \");\n        int number = input.nextInt();\n        System.out.println(\"You entered \" + number);\n\n        \/\/ closing the scanner object\n        input.close();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Enter an integer: 23\nYou entered 23\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an object named&nbsp;<var>input<\/var>&nbsp;of the&nbsp;<code>Scanner<\/code>&nbsp;class. We then call the&nbsp;<code>nextInt()<\/code>&nbsp;method of the&nbsp;<code>Scanner<\/code>&nbsp;class to get an integer input from the user.<\/p>\n\n\n\n<p>Similarly, we can use&nbsp;<code>nextLong()<\/code>,&nbsp;<code>nextFloat()<\/code>,&nbsp;<code>nextDouble()<\/code>, and&nbsp;<code>next()<\/code>&nbsp;methods to get&nbsp;<code>long<\/code>,&nbsp;<code>float<\/code>,&nbsp;<code>double<\/code>, and&nbsp;<code>string<\/code>&nbsp;input respectively from the user.<\/p>\n\n\n\n<p><strong>Note<\/strong>: We have used the&nbsp;<code>close()<\/code>&nbsp;method to close the object. It is recommended to close the scanner object once the input is taken.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"double-string-input\">Example: Get float, double and String Input<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\nclass Input {\n    public static void main(String&#91;] args) {\n    \t\n        Scanner input = new Scanner(System.in);\n    \t\n        \/\/ Getting float input\n        System.out.print(\"Enter float: \");\n        float myFloat = input.nextFloat();\n        System.out.println(\"Float entered = \" + myFloat);\n    \t\n        \/\/ Getting double input\n        System.out.print(\"Enter double: \");\n        double myDouble = input.nextDouble();\n        System.out.println(\"Double entered = \" + myDouble);\n    \t\n        \/\/ Getting String input\n        System.out.print(\"Enter text: \");\n        String myString = input.next();\n        System.out.println(\"Text entered = \" + myString);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Enter float: 2.343\nFloat entered = 2.343\nEnter double: -23.4\nDouble entered = -23.4\nEnter text: Hey!\nText entered = Hey!\n<\/samp><\/code><\/pre>\n\n\n\n<p>As mentioned, there are other several ways to get input from the user.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn simple ways to display output to users and take input from users in Java. Java Output In Java, you can simply use to send output to standard output (screen). Here, System&nbsp;is a class out&nbsp;is a&nbsp;public&nbsp;static&nbsp;field: it accepts output data. Don&#8217;t worry if you don&#8217;t understand it. We will discuss&nbsp;class,&nbsp;public, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[288],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1154"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1154"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1154\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}