In any programming language, input and output (I/O) is a key part of user interaction with your program. Input allows you to get user data while output allows you to display it.
As with most programming languages, the keyboard is the standard input device and the screen is the standard output device.
This guide looks at the basic I/O functions you can perform with Java.
Java Output
To show output on a screen, you can use the println() method. This method is in the System class.
Use the syntax below to display data:
System.out.println("Your output goes here.");
The above statement shows a field called out. This is a public static field that accepts the data to be output.
You also need to put quotes on the data you want to be shown. The exception to this is when the value in the System.out.println() statement is a variable or a number.
See the example below:
int t = 24;
System.out.println(t) // output is 24, not t
System.out.println(96)
Java also allows you to carry out arithmetic operations inside the println() method. You can add, subtract, divide or use modulus with this method. It's important to note that you aren't supposed to put quotes while using these arithmetic operations. Doing so will make the Java compiler, treat the expression as a string.
- System.out.println((9*6)/5); | Output got is the result of the arithmetic expression.
- System.out.println("(9*6)/5"); | Output got is the arithmetic expression and not the result.
The println() method is not the only Java method you can use to output data. The print() method can also be used to perform similar operations to println(). The only difference is that println() puts the cursor to the next line after printing, while print() leaves the cursor where output stopped.
The fully working code example below should help to ground the concepts above.
public class Output {
public static void main(String[] args) {
int age = 20;
System.out.println("Java ");
System.out.println("Programming");
System.out.print("Java ");
System.out.print("Programming");
System.out.println("Java is more than " + age + "years old."); // Line 8
}
}
Line 8 introduces the concatenation operator (+). Concatenation means to join. Therefore, that operator (+) is used to join different parts of the output.
From earlier, recall that quotes are not put on variables inside the System.out.println() statement. Line 8 shows how the concatenation operator enables you to meet this condition.
Java Input
Java provides several ways of getting user input but the Scanner class is used here.
To access the Scanner class, you need to import it.
import java.util.Scanner;
You then need to create an object of the Scanner class. This object can then be used to input data.
- Scanner input = new Scanner ( System.in); | Creating an object called input.
See the example below:
import java.util.Scanner;
class Output{
public static void main (String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer");
int n = input.nextInt(); // Line 5
if ((n%2)==0){
System.out.println("Your number is even");
}else{
System.out.println("Your number is odd");
input.close(); // Line 10
}
}}
The above code takes in an integer from a user and then tells them if it's even or odd.
Line 5 shows the method nextInt(). This method is used to get an integer input.
If you wanted to capture a String, float, or long data type, then you would use next(), nextFloat(), and nextLong() methods respectively.
On line 10, there's the close() method. It closes the Scanner class. It's advisable to always close the Scanner class when you're done using it.
Now You Know More About Input and Output on Java
In the last code example in this article, the if statement was used. It's one of the three program control structures in Java. In particular, it's a selection statement.
Selection statements are important to choose an execution path given a true or false condition. And now you know a bit more about input and output in Java, why not expand your knowledge on this programming language in other areas?
0 Comments