java


Table with a for loop and formatting


System.out.println("Year StartBalance Interest Earned End Balance");
for (int i = 1; i<Years+1; i++) { //table: year number, principal at beginning, total interest earned, principal at the end
double AmountPerQuarter = (Principal * (1 + Rate/400));
NewPrincipal = AmountPerQuarter;
double InterestEarned = NewPrincipal - Principal;
System.out.printf(i+ "%13s %15s %22s",Principal,InterestEarned,NewPrincipal);
Principal = NewPrincipal;
}
Hi, I'm a computer science student and I was a little bit confused on formatting in this example.
What this is supposed to do is, create the the table with Year, StartBalance, etc on the top, and then print the segment out as one line, and then when the loop repeats, it would start a new line, but using printf, everything the loop does is just printed on one line, which counteracts what I'm trying to do with the table.
For example, it ends up like:
Year StartBalance InterestEarned Endbalance
1..........500................20............520252040580
instead of starting a new line for the second year.
Also, as a side question, when I'm using formatting like "%13s" to create 13 spaces, how would I make it so what it prints is rounded to two decimal places? I know "%.2f" is whats used, but I'm not sure how I would combine both of them so I can create 13 spaces and have the number with rounded decimal spaces.
I'm sorry if this seems unclear, I've tried searching but couldn't find an answer.
Thank you!
To create a new line, you need a new line character, which could be created by adding '\n' or using a blank System.out.println(""); (which creates a new line character). Here's the former:
Change
System.out.printf(i+ "%13s %15s %22s",Principal,InterestEarned,NewPrincipal);
To
System.out.printf(i+ "%13s %15s %22s\n",Principal,InterestEarned,NewPrincipal);
You need %n for newline, also an integer is %d. Something like
System.out.printf("%12d %13s %15s %22s%n", i, Principal, InterestEarned,
NewPrincipal);

Related Links

Java generics: Generic parameter defined at method level vs interface level
if list is empty in that case how can i avoid null pointer exception in java?
Want to set value in property file from pom build using maven resource plugin
Maven build with AnnotationProcessor that parses files in src/main/java and generates sources to generated-test-sources/test-annotations
Not able to retrieve all the cookies
Want to convert a string as the coding convention
Populate a text area by clicking an item on a list
Richface: a4j:mediaOutput, data is 'null'
.exe file not printing output in jsp page
Annotations help in java
Android HashSet cannot be cast to LinkedHashSet
Why doesn't Roo generate Spring repository query methods for finder
JAXWS - SOAP WebService - Null object on client despite the XML response from WebService
Executing a perl script using PL/SQL
Java Game - Key Pressed
Why will my for loop not read my last char of the string?

Categories

HOME
testing
yii2
omnet++
oracle11g
heroku
comparison
include
stock
google-docs
adb
serverspec
enterprise-library-5
flyway4
ios-charts
upload
slick-slider
facebook-php-sdk
nano-server
lombok
nhibernate-envers
alpine
introduction
chromebook
minitab
neo4j-spatial
sql-server-2012-express
frame
binary-data
google-cloud-endpoints-v2
preg-match-all
twilio-api
code-contracts
stringtemplate
automake
uft-api
impersonation
galen
bootstrap-dialog
slick-3.0
skeleton-css-boilerplate
logfiles
user-accounts
prolog-setof
boost-multi-index
setter
google-web-starter-kit
pagerank
xna-4.0
execute
mutation-observers
iis-arr
jqgrid-formatter
tarjans-algorithm
varargs
event-bubbling
appfabric-cache
sorl-thumbnail
modalpopup
codeigniter-url
client-side-templating
java-metro-framework
openexr
dml
algebraic-data-types
html5-notifications
ms-project-server-2010
html-editor
flashvars
path-separator
sublist
trusted
datawindow
buildr
mercurial-server
inotifycollectionchanged
oncheckedchanged
locate
gdlib
jmock

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App