File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Adding nested loop in order to print out a table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding nested loop in order to print out a table" Watch "Adding nested loop in order to print out a table" New topic

Adding nested loop in order to print out a table

dylan terry

Joined: May 11, 2011
Posts: 1
My final project for my university is to enhance my code in order to have it print out the total price including the discount but before shipping charges and taxes. The rows should be items sold and the columns should be the wait of the items. I am starting to get frustrated, been at this for hours cant seem to figure it out. As a hint he says to use nested loops?

Heres my codes I have 1 main class and 1 test class

and my Main Class

Thanks for all the help ive been really struggling!

[HENRY: Fixed code tags. And did some formatting]
Steve Fahlbusch

Joined: Sep 18, 2000
Posts: 601

Greetings and welcome.

-- now that the greetings are done.

you should use code tags for your code --- please read the FAQs prior to posting.

you did not pose a question --- this is not a codemill ---- please read the FAQs prior to posting.

Please focus and give us a question with your understanding and we shall try to point you in the proper direction.

After you have read the FAQs, you may want to answer the following questions for us:

Where are you having difficulty?

Why are you having this difficuly?

How would you like to approch this problem?

And what do you see as going wroing with your approch?

Jared Malcolm
Ranch Hand

Joined: May 02, 2011
Posts: 54

dylan terry wrote: As a hint he says to use nested loops?

I don't see where you've followed his hint first off.....

SCJA 6 (Studying for SCJP 6)
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

No offense, but your code is kind of a mess... You don't follow conventions, so it gets very confusing. For example, when I see "Items", Since the first letter is capitalized, I think it is a class, not a reference.

You have a class called "DataEquations", but then make a reference variable it called 'price'. A price is not an equation, or a set of equations...a price should be (at least in my mind) a number.

On line 21 of your PriceCalculator class, you have a while loop, with an...odd...condition. I'm not saying it is wrong, but it doesn't look like anything that makes any sense to me. Why have these magic numbers?

Your while statement doesn't have any curly braces, so there is only one line in it's body. So let's assume I input a value of '2'. Since 2 is not <= 0, or > 10000000 (however many 0's you have), it skips the loop body, which is only the line 24. Then lines 26-45 all run.

Now, let's assume i put in a value of "-2". This makes the condition TRUE, and now i'm stuck in an infinite loop printing "Base Price = $-84.00" (or whatever price I input) forever, since you never change anything that would affect your condition.

so...I am assuming you need some major revisions to your code here. I would guess that you are supposed to ask the user how many items to ship (that part seems to work). I would then guess that you need a loop that gets the input weight of each and every item, one at a time, an stores all those values in a collection of some kind (and Array or ArrayList would work fine).

Once you have that done, I would work on printing out the data for each element.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Adding nested loop in order to print out a table
It's not a secret anymore!