Java || Snippet – Custom Setw & Setfill Sample Code For Java

This page will consist of a brief implementation of setw/ios::width and setfill/ios::fill in Java.
If you are a C++ programmer, no doubt you have used the setw and setfill commands many times. It makes formatting output like this very clean and simple (see below).
Ending balance:................. $ 6433.47
Amount of deposits:............. $ 1750.00
Amount of withdrawals:.......... $ 420.00
Amount of interest earned:...... $ 103.47
But to my amazement, I could not find a very suitable replacement for the setw/setfill functions in Java. Using simple for loops, the methods provided on this page has the ability to mimic both functions which are available in C++.
Included in the sample code are the following:
== SETW ==
(1) setwL - Right justifies string data of size "width," filling the width to the left of the string with whitespace.
(2) setwR - Left justifies string data of size "width," filling the width to the right of the string with whitespace.
== SETW/SETFILL ==
(3) setwLF - Right justifies string data of size "width," filling the width to the left of the string with a custom filler character.
(4) setwRF - Left justifies string data of size "width," filling the width to the right of the string with a custom filler character.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
// ============================================================================ // Author: K Perkins // Date: Jun 6, 2012 // Taken From: http://programmingnotes.org/ // File: Setw.java // Description: Demonstrate the use of custom setw/ios::width and // setfill/ios::fill functions for text formatting in Java. // ============================================================================ public class Setw { // ==== setwL ====================================================================== // // - Set field width left - // // This method mimics the C++ 'setw' function, which is used to format data to the // screen. This function right justifies string data of size "width," filling // the width to the left of the string with whitespace (' '). // // - USAGE - // setwL(<String> the text you wish to format, <int> size of width to be formated); // // ================================================================================= public void setwL(String str, int width) { for (int x = str.length(); x < width; ++x) { System.out.print(' '); } System.out.print(str); }// end of setwL // ==== setwR ====================================================================== // // - Set field width right - // // This method mimics the C++ 'setw' function, which is used to format data to the // screen. This function left justifies string data of size "width," filling // the width to the right of the string with whitespace (' '). // // - USAGE - // setwR(<String> the text you wish to format, <int> size of width to be formated); // // ================================================================================= public void setwR(String str, int width) { System.out.print(str); for (int x = str.length(); x < width; ++x) { System.out.print(' '); } }// end of setwR // ==== setwLF ====================================================================== // // - Set field width left fill - // // This method mimics the C++ 'setw' & 'setfill' functions, which are used to format // data to the screen. This function right justifies string data of size "width," // filling the width to the left of the string with a filler character. // // Use this method (instead of using 'setwL/setwR') when you want so specify the // type of filler you want to use // // - USAGE - // setwLF(<String> the text you wish to format, // <int> size of width to be formated, <char> the type of filler to be displayed); // // ================================================================================= public void setwLF(String str, int width, char fill) { for (int x = str.length(); x < width; ++x) { System.out.print(fill); } System.out.print(str); }// end of setwLF // ==== setwRF ====================================================================== // // - Set field width right fill - // // This method mimics the C++ 'setw' & 'setfill' functions, which are used to format // data to the screen. This function left justifies string data of size "width," // filling the width to the right of the string with a filler character. // // Use this method (instead of using 'setwL/setwR') when you want so specify the // type of filler you want to use // // - USAGE - // setwLF(<String> the text you wish to format, // <int> size of width to be formated, <char> the type of filler to be displayed); // // ================================================================================= public void setwRF(String str, int width, char fill) { System.out.print(str); for (int x = str.length(); x < width; ++x) { System.out.print(fill); } }// end of setwRF }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
Also, you must understand minimal object oriented programming to use this code!
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
===== DEMONSTRATION HOW TO USE =====
Use of the above snippet is similar to its C++ counterpart. Here is a sample program demonstrating its use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
public class Main { public static void main(String[] args) { // declare entry point to the 'Setw" class Setw format = new Setw(); // this formats text, printng whitespace to the left of the string System.out.print("1)"); format.setwL("This is whitespace text to the left", 45); System.out.println(""); // this formats text, printng whitespace to the right of the string System.out.print("2)"); format.setwR("This is whitespace text to the right", 45); System.out.print("!"); System.out.println(""); // this formats text, but this time, instead of printng whitespace // to the left of the string, it prints a "filler," which can be anything // you want. Currently, the filler is a exclaimation ('!') System.out.print("3)"); format.setwLF("This is filler text to the left", 45,'!'); System.out.println(""); // this formats text, but this time, instead of printng whitespace // to the right of the string, it prints a "filler," which can be anything // you want. Currently, the filler is a dollar sign ('$') System.out.print("4)"); format.setwRF("This is filler text to the right", 45,'$'); System.out.println(""); // you can also send numbers to any of the functions, // provided you first convert them to strings System.out.print("5)"); format.setwRF("This is a string with a number "+Integer.toString(1987), 45,'.'); System.out.println("n"); // you can also use this method to print the famous "triangle" shapes System.out.println("6) This is a triangle printed using the setwLF method"); for(int width=0; width<=10; ++width) { format.setwLF("", width,'*'); System.out.println(""); } System.out.println(""); // this prints an upside down triangle System.out.println("7) This is an upside down triangle printed " +"using the setwRF methodn"); for(int width=10; width>0; --width) { format.setwRF("", width,'*'); System.out.println(""); } }// end of main }// http://programmingnotes.org/ |
Once compiled, you should get this as your output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
1) This is whitespace text to the left 2)This is whitespace text to the right ! 3)!!!!!!!!!!!!!!This is filler text to the left 4)This is filler text to the right$$$$$$$$$$$$$ 5)This is a string with a number 1987.......... 6) This is a triangle printed using the setwLF method * ** *** **** ***** ****** ******* ******** ********* ********** 7) This is an upside down triangle printed using the setwRF method ********** ********* ******** ******* ****** ***** **** *** ** * |
Leave a Reply