java - encountering getting desired output -
public class dog { string name; public void bark() { system.out.println(name+ "says warf!" ); } public static void main(string[] args) { dog dog1 = new dog(); dog1.bark(); dog1.name="bart"; //creation of array dog[] dogs= new dog[3]; //object reference dogs[0]= new dog(); dogs[1]= new dog(); dogs[2]= dog1; //accessing object variables dogs[0].name= "fred"; dogs[1].name= "marge"; system.out.println("last dog's name is"); system.out.println(dogs[2].name); //looping through array int x=0; while(x < (dogs.length)); { dogs[x].bark(); x=x+1; } }
}
hello everybody.... new java , been rookie in java programming.... in abouve code....as per "head first java "textbook... output should be
"null says warf! last dog's name bart fred says warf! marge says warf!"
but in above code coded in eclipse ide.....first 2 lines of output m getting not last two....it seems while loop not getting executed..... can anybode tell me whats problem code reference output???
you have semi-colon @ end of while
statement:
while(x < (dogs.length)); // remove semi-colon
it seems while loop not getting executed.
in fact, execute infinitely, since value of x
same, , don't have body of while due semi-colon, change it. following code local scoped block, unrelated while
.
Comments
Post a Comment