Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Need help with Loops

Greenhorn
Posts: 13
Hello All,

I'm trying to figure out the following problem. I have the first part working correctly but it's the second part that is stumping me. It's a Kilograms to pounds console program that should have the following output. with the exception that the second set of pound and kilograms should be listed for every 5 kilograms like the second example and they have to be side by side tables.

Kilograms Pounds Pounds Kilograms
---------------------------------------------------------
1 2.2 2.2 1
3 6.6 6.6 3
5 11 11 5
7 15.4 15.4 7

Should be:

Kilograms Pounds Pounds Kilograms
---------------------------------------------------------
1 2.2 xxxx 5
3 6.6 xxxx 10
5 11 xxxx 15
7 15.4 xxxx 20

Here is the code I have:

Bartender
Posts: 1812
12
Try commenting out line 17, that'll get rid of the pounds outputting twice. See what it looks like and add more tabs if necessary.

I'm not sure what you mean by side by side tables... like on the right it needs to multiply both values by 5? If you use the code tag for the output it will keep the spacing and maybe be more descriptive... [EDIT] ohohoh I see.....

Maybe doing a second calculation inside the if statement:

Is that it?

Mitch Kazlo
Greenhorn
Posts: 13
The 2 table side by side should look like this:

KG LBS LBS KG
_________________
1 2.2 11.0 5
3 6.6 22.0 10
5 11.0 33.0 15
7 15.4 44.0 20

The second table to the right increments by 5, the first one on the left checks to see if i is divisible by 2 therefore only printing the odd numbers for KG's. I need to figure out how to create the second set of numbers right next to the first set which I think should be checking to see if i % 5 == 0 but every time I try to do that I get bad results. I've tried another if statement.

Janeice DelVecchio
Bartender
Posts: 1812
12
This is what I was trying to say before. If you look for a % 5 = 0, it misses all the ones that end in 5.

[EDIT] fixed typos

Marshal
Posts: 57482
175
Welcome to JavaRanch

Why are you using DecimalFormat; that went out of fashion 5 years ago when Formatter and the printf and format methods in System.out were introduced?

Mitch Kazlo
Greenhorn
Posts: 13
Thanks a bunch Janeice. I made a couple of modifications and this code is working to solve the problem.

Campbell, Thanks for the advice. I'll try those.

Janeice DelVecchio
Bartender
Posts: 1812
12

 We begin by testing your absorbancy by exposing you to this tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS