Format String for even columns


/ Published in: Java
Save to your folder(s)

this is a snippet from a parsing piece of code I had to do for class, basically I had to grab employee names, numbers, paycodes, hours worked, etc. and organize it. In order to align the columns I ended up using some code like this


Copy this code and paste it in your HTML
  1. for(i = 0; i < lastName.length; i++){
  2. chars[i] = (firstName[i] + lastName[i]).length();
  3. chars[i] = 17 - chars[i];
  4. //use larger number if strings are bigger
  5. spaces[i] = "";
  6. while (chars[i]>0){
  7. chars[i]--;
  8. spaces[i] += " ";
  9. }
  10. newArray[i] = lastName[i] + ", " + firstName[i] + spaces;
  11. /*
  12. * Nick Stanish
  13. * newArray[i] is the formatted column, if the arrays are confusing
  14. * then replace with a single string for easer readability
  15. */
  16. }
  17. //display
  18. System.out.println(newArray[0] + "other string");

URL: javafilter.heliohost.org

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.