Java Input/Output
When we create a program, one of the most fundamental concepts is handling input and output. A program should be able to accept input dynamically from different sources such as a keyboard, file, or network, and send output to various destinations such as the console, files, or sockets.
This is one of the first and most important topics for beginner programmers.
In this section, we will learn how to take input from the keyboard and display output on the console.
Output:
We have different classes, objects, and methods to display output on the console. Simplest way to print is System.out.println("output message").
Example:
public class HelloWorld {
public static void main(String[] args) {
String welcomeMessage = "Welcome to Java World";
System.out.println(welcomeMessage);//output: Welcome to Java World
}
}System.out : System.out is a built-in object in Java that is used to display data on the standard output device, usually the console.
println() :Prints text on the console and then moves the cursor to the next line.
print() :It also prints text on the console, but the cursor stays on the same line, so the next output continues from there.
Input:
To receive input from the user through the keyboard, Java provides different classes, objects and methods. In this section, we will look at the Scanner class and its methods to understand how it takes input from the user.
Example:
import java.util.Scanner;
public class helloWorld {
public static void main(String[] args) {
// Create a Scanner object to read input from the console
Scanner scanner = new Scanner(System.in);
// Ask user for input
System.out.print("Enter welcome message to display: ");
// Read input from user
String name = scanner.nextLine();
// Display output in console
System.out.println("Hello, " + name + "!");
// Close the scanner
scanner.close();
}
}Scanner : It is a java predefined class.
System.in : It is the standard input stream, that is the keyboard.
nextLine() : This method reads an entire line of input entered by the user.
nextInt() : This method reads whole number (e.g., 1, 45, 100) entered by the user.
nextDouble() : This method reads decimal values (e.g., 100.5, 999.99) entered by the user.
next().charAt(0)() : This method reads Reads first character from a string only entered by the user.
nextBoolean() : This method accepts true or false entered by the user.
Example:
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.print("Enter salary: ");
double salary = sc.nextDouble();
System.out.print("Enter grade: ");
char grade = sc.next().charAt(0);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Grade: " + grade);
sc.close();
}
}Example-2
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input first number
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
// Input second number
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
// Add numbers
int sum = num1 + num2;
// Display result
System.out.println("Sum = " + sum);
sc.close();
}
}See you in next page....🙂