aspose file tools*
The moose likes Beginning Java and the fly likes Newby needs help with a  problem (no error) regrading for loops or if else Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Newby needs help with a  problem (no error) regrading for loops or if else" Watch "Newby needs help with a  problem (no error) regrading for loops or if else" New topic
Author

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

Timothy Onggowasito
Greenhorn

Joined: Apr 16, 2010
Posts: 11
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.


Apparently, what you don't know can hurt you...
Tim Ong
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Dec 30, 2009
Posts: 62
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.


SCJP6
Timothy Onggowasito
Greenhorn

Joined: Apr 16, 2010
Posts: 11
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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2885
    
  13

"Anoo Kota", please check your private messages regarding an important administrative matter from JavaRanch.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
My Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
 
wood burning stoves
 
subject: Newby needs help with a problem (no error) regrading for loops or if else
 
Similar Threads
Reference variable
Something I just dont get about the OOP concept
Inheritance, overriding methods
basic object question
begin execution of a thread -- start or run?