Page 49 - CTS - CSA TP - Volume 2
P. 49
COMPUTER SOFTWARE APPLICATION - CITS
Explanation:
• The program checks whether the console object is null.
• If console is null, it means that the console is not available, and the program prints an error message.
• The System.exit(1); statement is used to exit the program with a status code of 1, indicating an abnormal
termination.
Step 5: Prompt the User to Enter a City
Explanation:
• The console.readLine(“Enter your favorite city: “); statement prompts the user to enter their favorite city.
• The readLine method of the Console class is used to read a line of text from the console. It displays the
specified prompt and waits for the user to enter input.
• The entered text is then assigned to the variable city.
Step 6: Display the Entered City
Explanation:
• After the user enters their favorite city, the program displays the entered city using the System.out.println
statement.
• The println method is used to print the message “Your favorite city is: “ followed by the value stored in the city
variable.
• This line effectively prints the user’s favorite city to the console.
Output:
TASK 6: Reading Password with Console Class Example_2
import java.io.Console;
public class ConsoleExample2 {
public static void main(String[] args) {
Console console = System.console();
34
CITS : IT & ITES - Computer Software Application - Exercise 84