JAVA (Mimggu 5)
JAVA (Mimggu 5)
Remember to handle exceptions that might occur when working with user input, such
as ‘InputMismatchException’ when trying to read an integer and the user enters non-
integer data.
Additionally, be cautious when closing the ‘System.in’ input stream, especially in
more complex applications. Closing ‘System.in’ can have unintended consequences,
so it's often better to manage the lifecycle of the ‘Scanner’ object without closing
‘System.in’.
Output:
The value of number is: 42
5.2.4 Formatting Output:
You can use ‘System.out.printf()’ to format output with placeholders, similar to
the ‘printf’ function in C. Example:
double price = 19.99;
System.out.printf("The price is %.2f dollars%n", price);
Output:
The price is 19.99 dollars
5.2.5 Escape Sequences:
Java supports escape sequences for special characters. For example, ‘\n’
represents a newline character, ‘\t’ represents a tab character, and ‘\\’ represents a
literal backslash. Example:
System.out.println("Line 1\nLine 2\tTabbed");
Output:
Line 1
Line 2 Tabbed