Tuesday, May 27, 2014

How can I make a table nice and neat.


So I just processed this rectangular array and made myself a table. I want to put a label for each column to identify what the values are. My attempt at this failed miserably, because it looks horrible. Here is the necessary code.



Java Code:



System.out.println();
System.out.println("FORMATTED RESULTS");
System.out.println(results);

values[rowCount][0] = currency.format(monthlyInvestment);
values[rowCount][1] = percent.format(interestRate/100);
values[rowCount][2] = Integer.toString(years);
values[rowCount][3] = currency.format(futureValue);
rowCount++;

// see if the user wants to continue
System.out.print("Continue? (y/n):");
System.out.println();
choice = sc.next();
}

System.out.println("RECORED RESULTS \n MontlyInvestment Yearly Interest Rate Years Future Value");
valuesProcessor:
for(String[] row : values)
{
for(String col : row)
{
if(col == null)
break valuesProcessor;
else
System.out.print(col + " ");
}
System.out.println();
}
}

If you are to compile this yourself, you would see the horrible output at the end. I would like to know how to properly align each label with its corresponding column.

No comments:

Post a Comment