|
| 1 | +--- |
| 2 | +title: Java Basic Input and Output |
| 3 | +description: In this tutorial, you will learn simple ways to display output to users and take input from users in Java. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | + |
| 8 | +## Java Output |
| 9 | +In Java, you can simply use |
| 10 | + |
| 11 | +```java |
| 12 | +System.out.println(); or |
| 13 | + |
| 14 | +System.out.print(); or |
| 15 | + |
| 16 | +System.out.printf(); |
| 17 | +``` |
| 18 | + |
| 19 | +to send output to standard output (screen). |
| 20 | + |
| 21 | +Here, |
| 22 | + |
| 23 | +- `System` is a class |
| 24 | +- `out` is a `public` `static` field: it accepts output data. |
| 25 | + |
| 26 | +Don't worry if you don't understand it. We will discuss `class`, `public`, and `static` in later chapters. |
| 27 | + |
| 28 | +Let's take an example to output a line. |
| 29 | + |
| 30 | +```java |
| 31 | +class AssignmentOperator { |
| 32 | + public static void main(String[] args) { |
| 33 | + |
| 34 | + System.out.println("Java programming is interesting."); |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | +**Output:** |
| 39 | + |
| 40 | +```plaintext |
| 41 | +Java programming is interesting. |
| 42 | +``` |
| 43 | +Here, we have used the `println()` method to display the string. |
| 44 | + |
| 45 | +## Difference between println(), print() and printf() |
| 46 | +- `print()` - It prints string inside the quotes. |
| 47 | +- `println()` - It prints string inside the quotes similar like `print()` method. Then the cursor moves to the beginning of the next line. |
| 48 | +- `printf()` - It provides string formatting. |
| 49 | + |
| 50 | +### Example: print() and println() |
| 51 | +```java |
| 52 | +class Output { |
| 53 | + public static void main(String[] args) { |
| 54 | + |
| 55 | + System.out.println("1. println "); |
| 56 | + System.out.println("2. println "); |
| 57 | + |
| 58 | + System.out.print("1. print "); |
| 59 | + System.out.print("2. print"); |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | +**Output:** |
| 64 | +```plaintext |
| 65 | +1. println |
| 66 | +2. println |
| 67 | +1. print 2. print |
| 68 | +``` |
| 69 | +In the above example, we have shown the working of the `print()` and `println()` methods. To learn about the `printf()` method, visit Java [**printf()**](https://www.cs.colostate.edu/~cs160/.Summer16/resources/Java_printf_method_quick_reference.pdf). |
| 70 | + |
| 71 | +### Example: Printing Variables and Literals |
| 72 | + |
| 73 | +```java |
| 74 | +class Variables { |
| 75 | + public static void main(String[] args) { |
| 76 | + |
| 77 | + Double number = -10.6; |
| 78 | + |
| 79 | + System.out.println(5); |
| 80 | + System.out.println(number); |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | +When you run the program, the output will be: |
| 85 | + |
| 86 | +```plaintext |
| 87 | +5 |
| 88 | +-10.6 |
| 89 | +``` |
| 90 | +Here, you can see that we have not used the quotation marks. It is because to display integers, variables and so on, we don't use quotation marks. |
| 91 | + |
| 92 | +### Example: Print Concatenated Strings |
| 93 | +```java |
| 94 | +class PrintVariables { |
| 95 | + public static void main(String[] args) { |
| 96 | + |
| 97 | + Double number = -10.6; |
| 98 | + |
| 99 | + System.out.println("I am " + "awesome."); |
| 100 | + System.out.println("Number = " + number); |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | +**Output:** |
| 105 | + |
| 106 | +```java |
| 107 | +I am awesome. |
| 108 | +Number = -10.6 |
| 109 | +``` |
| 110 | +In the above example, notice the line, |
| 111 | + |
| 112 | +```java |
| 113 | +System.out.println("I am " + "awesome."); |
| 114 | +``` |
| 115 | + |
| 116 | +Here, we have used the + operator to concatenate (join) the two strings: `"I am "` and `"awesome."`. |
| 117 | + |
| 118 | +And also, the line, |
| 119 | + |
| 120 | +```java |
| 121 | +System.out.println("Number = " + number); |
| 122 | +``` |
| 123 | +Here, first the value of variable `number` is evaluated. Then, the value is concatenated to the string: `"Number = "`. |
| 124 | + |
| 125 | +## Java Input |
| 126 | +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 `Scanner` class. |
| 127 | + |
| 128 | +In order to use the object of `Scanner`, we need to import `java.util.Scanner` package. |
| 129 | + |
| 130 | + |
| 131 | +```java |
| 132 | +import java.util.Scanner; |
| 133 | +``` |
| 134 | +To learn more about importing packages in Java, visit [Java Import Packages](). |
| 135 | + |
| 136 | +Then, we need to create an object of the `Scanner` class. We can use the object to take input from the user. |
| 137 | + |
| 138 | + |
| 139 | +```java |
| 140 | +// create an object of Scanner |
| 141 | +Scanner input = new Scanner(System.in); |
| 142 | + |
| 143 | +// take input from the user |
| 144 | +int number = input.nextInt(); |
| 145 | +``` |
| 146 | + |
| 147 | +### Example: Get Integer Input From the User |
| 148 | +```java |
| 149 | +import java.util.Scanner; |
| 150 | + |
| 151 | +class Input { |
| 152 | + public static void main(String[] args) { |
| 153 | + |
| 154 | + Scanner input = new Scanner(System.in); |
| 155 | + |
| 156 | + System.out.print("Enter an integer: "); |
| 157 | + int number = input.nextInt(); |
| 158 | + System.out.println("You entered " + number); |
| 159 | + |
| 160 | + // closing the scanner object |
| 161 | + input.close(); |
| 162 | + } |
| 163 | +} |
| 164 | +``` |
| 165 | +**Output:** |
| 166 | + |
| 167 | +```plaintext |
| 168 | +Enter an integer: 23 |
| 169 | +You entered 23 |
| 170 | +``` |
| 171 | +In the above example, we have created an object named `input` of the `Scanner` class. We then call the `nextInt()` method of the `Scanner` class to get an integer input from the user. |
| 172 | + |
| 173 | +Similarly, we can use `nextLong()`, `nextFloat()`, `nextDouble()`, and `next()` methods to get `long`, `float`, `double`, and `string` input respectively from the user. |
| 174 | + |
| 175 | +<Callout> |
| 176 | +**Note:** We have used the `close()` method to close the object. It is recommended to close the scanner object once the input is taken. |
| 177 | +</Callout> |
| 178 | + |
| 179 | +### Example: Get float, double and String Input |
| 180 | +```java |
| 181 | +import java.util.Scanner; |
| 182 | + |
| 183 | +class Input { |
| 184 | + public static void main(String[] args) { |
| 185 | + |
| 186 | + Scanner input = new Scanner(System.in); |
| 187 | + |
| 188 | + // Getting float input |
| 189 | + System.out.print("Enter float: "); |
| 190 | + float myFloat = input.nextFloat(); |
| 191 | + System.out.println("Float entered = " + myFloat); |
| 192 | + |
| 193 | + // Getting double input |
| 194 | + System.out.print("Enter double: "); |
| 195 | + double myDouble = input.nextDouble(); |
| 196 | + System.out.println("Double entered = " + myDouble); |
| 197 | + |
| 198 | + // Getting String input |
| 199 | + System.out.print("Enter text: "); |
| 200 | + String myString = input.next(); |
| 201 | + System.out.println("Text entered = " + myString); |
| 202 | + } |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +**Output:** |
| 207 | + |
| 208 | +```plaintext |
| 209 | +Enter float: 2.343 |
| 210 | +Float entered = 2.343 |
| 211 | +Enter double: -23.4 |
| 212 | +Double entered = -23.4 |
| 213 | +Enter text: Hey! |
| 214 | +Text entered = Hey! |
| 215 | +``` |
| 216 | +As mentioned, there are other several ways to get input from the user. To learn more about `Scanner`, visit Java Scanner. |
0 commit comments