Page 45 - CTS - CSA TP - Volume 2
P. 45
COMPUTER SOFTWARE APPLICATION - CITS
This line prints a greeting message to the console, incorporating the user’s entered name. The println method is
used to print the message and move to the next line.
Step 8: Closing the Scanner
This line closes the Scanner object to release associated resources. It’s good practice to close the Scanner when
it’s no longer needed.
Step 9: End of the Program
This closing brace marks the end of the main method and the end of the ScannerExample class.
Output:
Summary:
• The program starts by importing the Scanner class.
• It creates a Scanner object to read input from the keyboard.
• It prompts the user to enter their name, reads the input, and stores it in a variable.
• It displays a greeting message incorporating the user’s name.
• Finally, it closes the Scanner object.
TASK 2: Reading Multiple Inputs with Scanner Class Example_2
import java.util.Scanner;
public class SumOfTwoNumbers {
public static void main(String[] args) {
// Create a Scanner object to read input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the first number
System.out.print(“Enter the first number: “);
// Read the first number from the user
double num1 = scanner.nextDouble();
30
CITS : IT & ITES - Computer Software Application - Exercise 84