aspose file tools*
The moose likes Beginning Java and the fly likes Help with Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Loops" Watch "Help with Loops" New topic
Author

Help with Loops

benito kamelas
Greenhorn

Joined: Sep 29, 2013
Posts: 8
Hello everybody, so here is a bit of code for a homework I have for tomorrow. The problem I am having is that when I pick 9 products from the menu (Scenario 1), it gives me the total of what it cost. But when I pick only one product from the list (Check Scenario 2 & 3), it STILL gives me the total cost as if I had still picked the 9 products (Check last screenshot). Check images below to understand better.

Any help would be appreciated.

Thanks!







fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

I don't see the logic error, but I can give you some suggestions...

1) put in more System.out.println(); statements. You will take these out later, but these are GREAT for debugging. It will let you see what your code is really doing.

2) Simplify. Comment out all but (say) 2-3 choices. no need to clutter up your non-working code with more stuff that doesn't work. Get it working with 2-3, and then you can add in the remaining 7-8.

3) Consider using an array instead of 10 variables named nProductX. This kind of solution doesn't scale well. Imagine if you had 100 or 10,000 products.

4) Consider starting over. writing almost 200 lines of code only to find out it doesn't work is only going to lead to frustration. I NEVER write more than 2-3 before I recompile and TEST TEST TEST.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
benito kamelas
Greenhorn

Joined: Sep 29, 2013
Posts: 8
Thanks for the reply,

Appreciate your idea of putting arrays in it. Makes it more easier instead of putting it one by one.

Could you be more specific on the system print and how does it find the bugs? I mean, I know what its for but didn't know it worked as a debugger.

Have re done this homework and always tested it one by one, but the error looks like its coming from the first IF statement. Have tried to incorporate another IF statement within it or an ELSE IF to see if that works. Even I tried to put constant variable and it also didn't work. The reason I put the whole work as if it was done is to show and seek guiding on where might be the mistake.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Two obvious issues...

First, you ask for the item at the end of the loop -- and by the time you reach there, you processed every item on the list. So, you start off with every item purchased.

Second, the choice item number is incorrect. Choosing 1, or 2, or 3, or any number of the items on the list doesn't work. The check is by it's price, not by it's choice. So, if you want a gMac, you have to enter 1299, and not 6.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

benito kamelas wrote:Could you be more specific on the system print and how does it find the bugs? I mean, I know what its for but didn't know it worked as a debugger.

System.out.println (S.o.p) doesn't find bugs. Your BRAIN finds them. S.o.p just helps you see what is really happening so you can understand.

What I will often do is put in a statement just before each loop starts, and print out the value I'm about to test (this doesn't work on a for-loop, but will for a while).

Then, the first statement in the loop will again be the value. So just before your line 72, i'd do something like:



Before each if-statement in your loop, print nNumber and nProductX (which will be easier one you have an array). Then you can see what the values are, and why it is or isn't entering the if-statement.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Loops