Posted By

nickstanish on 02/21/12


Tagged

format String columns


Versions (?)

Format String for even columns


 / Published in: Java
 

URL: javafilter.heliohost.org

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

  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");

Report this snippet  

You need to login to post a comment.