Console output in Java can often require specific formatting for readability and presentation purposes.
To display numbers in a desired format.
Example:
double investment = 986.236548;
System.out.printf(format, item1, item2, ...);
System.out.format(format, item1, item2, ...);
format: a string that may consist of substrings and format specifiers.
format specifier: determines how an item should be displayed and begins with a percent sign (%) and can represent numeric values, characters, or Booleans.
Integer in 8 spaces:
System.out.printf("%8d", numCourses);
Floating-point numbers with 2 decimals in 10 spaces:
System.out.printf("%10.2f", cost);
String in 12 spaces:
System.out.printf("%12s", aWord);
%b: boolean value (true/false)
%c: character
%d: decimal integer
%f: floating-point number
%e: standard scientific notation
%s: string
%b: basic format
Example: System.out.printf("%b", isGrad);
%nb: format with n output spaces, right-justified
%c: basic format
Example: System.out.printf("%c", aCharacter);
%nc: n output spaces, right justified
%d: basic format
Example: System.out.printf("%d", k);
%nd: n output spaces, right justified
%f: basic format
Example: System.out.printf("%f", cost);
%n.kf: n output spaces, k digits after the decimal point, right justified
%e: basic format
Example: System.out.printf("%e", cost);
%n.kf: n output spaces, k digits after the floating point, right justified
%s: basic format
Example: System.out.printf("%s", aWord);
%ns: n output spaces, right justified
Automatically increases space if an item requires more than the specified width.
Format strings can include substrings for additional information.
Use "
" at the end of the format string to print a new line after the output.
Output alignment is right justified by default; left justification is indicated with a minus sign.
If an item requires more output spaces than specified, the width is adjusted automatically.
Use Calendar class to get current date/time and format output appropriately.
Example:
System.out.printf("%tB %te, %tY%n", b, b, b);
Introduce mathematical functions, characters, strings, and their application in Java.
The Math class is essential for performing mathematical operations.
Common Functions include pow(a, b), sqrt(a), and more.
Constants such as Math.PI, Math.E for various computations.
Represents a single character.
Character literals are enclosed in single quotes.
Unicode and ASCII are the common encoding schemes. Examples of how characters are represented in both formats.
Represents a sequence of characters, distinguished by enclosing in double quotes.
String methods can be performed like length(), charAt(index), toUpperCase(), etc.
Various exercises demonstrate the use of Java features such as mathematical computations, string manipulation, and user input reading.
Lecture 4(1)
Console output in Java can often require specific formatting for readability and presentation purposes.
To display numbers in a desired format.
Example:
double investment = 986.236548;
System.out.printf(format, item1, item2, ...);
System.out.format(format, item1, item2, ...);
format: a string that may consist of substrings and format specifiers.
format specifier: determines how an item should be displayed and begins with a percent sign (%) and can represent numeric values, characters, or Booleans.
Integer in 8 spaces:
System.out.printf("%8d", numCourses);
Floating-point numbers with 2 decimals in 10 spaces:
System.out.printf("%10.2f", cost);
String in 12 spaces:
System.out.printf("%12s", aWord);
%b: boolean value (true/false)
%c: character
%d: decimal integer
%f: floating-point number
%e: standard scientific notation
%s: string
%b: basic format
Example: System.out.printf("%b", isGrad);
%nb: format with n output spaces, right-justified
%c: basic format
Example: System.out.printf("%c", aCharacter);
%nc: n output spaces, right justified
%d: basic format
Example: System.out.printf("%d", k);
%nd: n output spaces, right justified
%f: basic format
Example: System.out.printf("%f", cost);
%n.kf: n output spaces, k digits after the decimal point, right justified
%e: basic format
Example: System.out.printf("%e", cost);
%n.kf: n output spaces, k digits after the floating point, right justified
%s: basic format
Example: System.out.printf("%s", aWord);
%ns: n output spaces, right justified
Automatically increases space if an item requires more than the specified width.
Format strings can include substrings for additional information.
Use "
" at the end of the format string to print a new line after the output.
Output alignment is right justified by default; left justification is indicated with a minus sign.
If an item requires more output spaces than specified, the width is adjusted automatically.
Use Calendar class to get current date/time and format output appropriately.
Example:
System.out.printf("%tB %te, %tY%n", b, b, b);
Introduce mathematical functions, characters, strings, and their application in Java.
The Math class is essential for performing mathematical operations.
Common Functions include pow(a, b), sqrt(a), and more.
Constants such as Math.PI, Math.E for various computations.
Represents a single character.
Character literals are enclosed in single quotes.
Unicode and ASCII are the common encoding schemes. Examples of how characters are represented in both formats.
Represents a sequence of characters, distinguished by enclosing in double quotes.
String methods can be performed like length(), charAt(index), toUpperCase(), etc.
Various exercises demonstrate the use of Java features such as mathematical computations, string manipulation, and user input reading.