File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newby needs help with a problem (no error) regrading for loops or if else

 
Timothy Onggowasito
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

This is my very first post so don't mind my stupidity
I'm also a pure beginner in Java.

Anyways I'm having trouble with the code listed below, to tell you the truth I'm not sure where the program is located.
The output of program is suppose to be:

Ooh! Loook its a cat!
Cat: meow

Would you like to kick the cat?
<input here>

so if you type in yes
the cat will infinitely meow at you
if you type in no
the cat will purr at you 5 times.





The problem is that if you type in no nothing happens. did I made a mistake in the for loop? or is the ifs?
Please help.
Thanks in advance.
 
pete stein
Bartender
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and welcome to the Ranch!

As for your question, please have a look here:


a standard for loop has 3 parts:



So for your loop you have:

initial condition: int i set equal to 0 -- No problem here.

test condition: repeat while i is greater than 5 -- do you detect any problem with logic here?

do once each time we loop: increase i by one -- no problem here.

Good luck and hope this helps!
 
Anup Om
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in for loop of the pur() method.



In the very first run of the for loop, i is 0. You are verifying if i is greater than 5, which if false. So, the code in the loop is never executed.

 
Timothy Onggowasito
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! I knew it was the for loops!
Thanks for your help, both of you.
Now I can finally get on with a new one.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4116
195
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Anoo Kota", please check your private messages regarding an important administrative matter from JavaRanch.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic