• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help My Project does not loop properly  RSS feed

 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the top of the page is a sample of how the program should run the loops are not working and its only executing one method. What is the problem with my code? Please Help!

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Meki, after entering input you are comparing it "Y" and if not equal then go ahead else terminate.



compareTo returns 0 if equals else other values. So when you entered "Y" then it returns 0. And as you put not equal check
then it goes else where you are terminating loop.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You don't want compareTo(). You use the correct method elsewhere in the program.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also at line 63, you are comparing with "N", if its not "N" then terminate the loop. So when you are enter "Y" it will be terminated.

you should correct that as well. Also, if you want to calculate the values again then you need to put everything in loop to check the condition..
 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Also at line 63, you are comparing with "N", if its not "N" then terminate the loop. So when you are enter "Y" it will be terminated.

You should correct that as well. Also, if you want to calculate the values again then you need to put everything in loop to check the condition..


whats a better way of doing it without termination the loop
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is this really doing what you want? If the answer is not "N", print the exit statement? What happens when answer is "Y"?
 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what should the first if else statement look like to work
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's what we're hoping you will figure out. Show us what you think will work.
 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Is this really doing what you want? If the answer is not "N", print the exit statement? What happens when answer is "Y"?


I was trying to make it loop back to the first question
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's backup a bit. First, you need to format your code correctly. There are a few ways to do that and here's one:

Now you can see that you're looping while the answer is not equal to "Y", but you haven't asked to the Y/N question yet.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meki Banda wrote:On the top of the page is a sample of how the program should run the loops are not working and its only executing one method. What is the problem with my code? Please Help!

In my opinion, the problem with your code is that you've written far too much of it. You've written a hundred lines and you're only now starting to test it - and that's a disaster waiting to happen.

Programming is (or should be) an incremental process, so if you write a loop, test it ... immediately. If you write a method, test it. And compile every ten lines you write (and some would say even less than that).

Yes, it means that you might have to write some code that you later remove (sometimes called "Kleenex code"), but you cannot expect to bash out a hundred lines of code and simply expect it to work.

Also: your code is very procedural. The only class you've written is the one you've been forced to; otherwise your program could have just as easily been written in C.

What about a Circle class that can return its own area? And maybe a Sphere class that can return its own area and volume? Do you see how that might be a better way to go?

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!