Page 47 - CTS - CSA TP - Volume 2
P. 47
COMPUTER SOFTWARE APPLICATION - CITS
Output:
TASK 4: Reading a floating-point number with Scanner Class Example_4
import java.util.Scanner;
public class ScannerExample4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a floating-point number
System.out.print(“Enter a floating-point number: “);
double floatingPointNumber = scanner.nextDouble();
// Display the entered number
System.out.println(“You entered: “ + floatingPointNumber);
// Close the Scanner
scanner.close();
}
}
Output:
TASK 5: Reading Text with Console Class Example_1
import java.io.Console;
public class ConsoleExample1 {
public static void main(String[] args) {
// Get the Console object
Console console = System.console();
if (console == null) {
System.out.println(“Console not available. Exiting...”);
System.exit(1);
32
CITS : IT & ITES - Computer Software Application - Exercise 84