KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm a beginner to java and this is my first post on Stackoverflow. Although my initial code is similar to other posts here, my question relates to implementing StringBuilder and so the reason for this post. I've created a method **Initial code** private static String padAgain(String result,int padNum) { String str = ""; for(int i = padNum; i >= 0; i--) { str = String.format("%" + padNum + "s", result).replace(" ", "*"); } return str; } to left pad the string with blank spaces (or special characters) using String.format method. I'm using the same in my program to right justify numbers. Numbers start with 2 (single-digit) and increase in multiples of 2 (containing n no. of digits) with the spaces being inversely proportional to the increase in digits. Here is the output for illustrative purposes: Enter exponent value for base-2 - Enter 0 to exit: 128 Square 1: **************************************2 items Square 2: **************************************4 items Square 3: **************************************8 items Square 4: *************************************16 items Square 5: *************************************32 items Square 6: *************************************64 items Square 7: ************************************128 items Square 8: ************************************256 items Square 9: ************************************512 items Square 10: ***********************************1024 items Square 11: ***********************************2048 items Square 12: ***********************************4096 items Square 13: ***********************************8192 items Square 14: **********************************16384 items Square 15: **********************************32768 items Square 16: **********************************65536 items Square 17: *********************
Tags (comma-separated)
Save Edits
Cancel