This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Need help with Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with Loops" Watch "Need help with Loops" New topic
Author

Need help with Loops

Mitch Kazlo
Greenhorn

Joined: Oct 30, 2009
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:



Any help you can give me is greatly appreciated.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

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?


When you do things right, people won't be sure you've done anything at all.
Mitch Kazlo
Greenhorn

Joined: Oct 30, 2009
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
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

Joined: Oct 30, 2009
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with Loops
 
Similar Threads
loop over array to check for duplicates
Section 2 mock test questions
Do while error, I'm going crazy
Getting rid of unwanted decimal numbers
August Newsletter Puzzle