• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Adding nested loop in order to print out a table  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
Posts: 612
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Ranch Hand
Posts: 54
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

I don't see where you've followed his hint first off.....
lowercase baba
Posts: 12627
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!