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

Can outclass specify inner class with InnerClassNameOuter directly rather than ClassName.InnerClassName
Thermal Printer Bixolon SRP350 Java Prints Arabic Splitted Characters
JUnit Test Cases for Config
Spring/Hibernate/Oracle: ORA-02289 Sequence Does Not Exist?
Unclear use of int variable - official Oracle example Using Byte Streams [duplicate]
Java Threads producer consumer program
Store Highest Score and Display It
Execution failed for task app:compileDebugJavaWithJavac
US Robotics 56K USB Modem - Caller ID (Android)
How to create a Jar-Library of reusable sources with gradle?
javax.mail not sends emails and not showing any error
what should I do, If I want to insert integer data in ms access? [duplicate]
How to save multiple ImageViews as one Bitmap while maintaining position of each ImageView in Android?
Adding & moving columns doesn't completely remove the column from its original position in JTable [duplicate]
JBoss EAP 6.2 CDI beans.xml
Persist data into BLOB / CLOB datatype in Oracle DB using Apache Camel Framework

Categories

HOME
log4j
gitlab
webpack
netsuite
amazon-ec2
activiti
isabelle
tinymce
spagobi
yarn
sharepoint-designer
yum
indesign
qore
paradox
synchronization
applepay
primary-key
nano-server
spring-xd
google-cloud-ml
usergrid
orchardcms
caml
workload-scheduler
evopdf
contextmenu
arabic
tar
sparse-matrix
pingfederate
yadcf
noraui
repo
host
vxworks
gesture
service-discovery
code-search-engine
angularjs-factory
g-code
elmah
botbuilder
heightmap
auto-update
azure-application-gateway
sage-one
domain-model
integrity
mu
knockout-components
qtwebview
django-scheduler
minimization
abcpdf9
elements
home-directory
impresspages
ipconfig
rhino-servicebus
asp.net5
xpath-1.0
pyke
browser-link
kendonumerictextbox
ora-00900
collapse
quicklisp
sgen
comexception
kgdb
xsockets.net
tidy
inbox
blending
mylyn
jelly
cassini-dev
rdoc
parametric-equations
chrono
qtkit
pendrive
assembly-loading
pydot
doh
zpt
external-accessory
mysql-error-1005
web-application-design
firefox-5
substrings
memory-size
dbisam

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