Page 48 - CTS - CSA TP - Volume 2
P. 48
COMPUTER SOFTWARE APPLICATION - CITS
}
// Prompt the user to enter a city
String city = console.readLine(“Enter your favorite city: “);
// Display the entered city
System.out.println(“Your favorite city is: “ + city);
}
}
Step 1: Import Console Class
Explanation:
• The import java.io.Console; statement is used to import the Console class from the java.io package.
• The Console class provides methods for interacting with the console, allowing secure input without echoing
characters (useful for reading sensitive information like passwords).
Step 2: Define Class and Main Method
Explanation:
• The code defines a class named ConsoleExample1.
• Inside the class, there is the main method, which serves as the entry point of the program. The main method
is the first method that gets executed when the program runs.
Step 3: Get Console Object
Explanation:
• The System.console() method is used to obtain a reference to the console.
• If the console is not available (for example, if the program is running in an environment without a console, such
as some IDEs), the method returns null.
Step 4: Check for Console Availability
33
CITS : IT & ITES - Computer Software Application - Exercise 84 CITS : IT & ITES - Computer Software Application - Exercise 84