4/1/2023 0 Comments Tabular formatUsing () to Print a List as a Table in Java Import import public class TableExample The method format("%10s %20s %5d %5c",student.getId(), student.getName(), student.getAge(), student.getGrade()) also has the format specifier and getter methods of the student class to get the value of the attributes. The println() method moves the cursor to the next line after printing the result. Thus, %10s, for example, formats a string with a specified number of characters and also right justify it. The method printf("%10s %20s %5s %5s", "STUDENT ID", "NAME", "AGE", "GRADE") has the format specifier to format the arguments passed. Hence, in this code, we have used a combination of width and conversion-character to format the given student’s data into a table. The s formats strings while d formats decimal integers, and the result of c is a Unicode Character. Here we have used two of the common ones, s, d, and c. The conversion-character determines how the string is formatted. The printf internally uses to parse the format string and output it. Specifiers given in the bracket are optional. The formatting string consists of literals and format specifiers, which include flags, width, precision, and conversion character in this sequence. Here we have a POJO class Student with basic attributes like id, name, age and grade and the TestExample class where we create a List of students and print that information in a tabular format. We can specify the rules for formatting using format patterns and the rules start with %. The printf(format, arguments) method provides String formatting.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |