Reputation: 1
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.... i am new to java and been rookie in java programming.... in the abouve code....as per "head first java "textbook... the output should be
"null says warf!
last dog's name is bart
fred says warf!
marge says warf!"
but in the above code as i coded it in eclipse ide.....first two lines of output m getting but not the last two....it seems that the while loop is not getting executed..... can anybode tell me whats the problem with the code with reference to the output???
Upvotes: 0
Views: 56
Reputation: 213193
You have a semi-colon at the end of the while
statement:
while(x < (dogs.length)); // Remove the semi-colon
it seems that the while loop is not getting executed.
In fact, it will execute infinitely, since the value of x
is always the same, and you don't have any body of the while due to that semi-colon, to change it. The following code is just a local scoped block, unrelated to while
.
Upvotes: 4