Reputation: 1
I am trying to iterate through this array of objects, but I keep getting "; expected" when I try and make a for loop to do so within the constructor.
public class EletronicStore {
public EletronicStore() {
Object[] obj = new Object[9];
Desktop d1 = new Desktop(3.5, 8, 500, false);
Desktop d2 = new Desktop(3, 16, 250, true);
Desktop d3 = new Desktop(4.3, 32, 500, true);
Laptop l1 = new Laptop(3.1, 32, 500, true, 15);
Laptop l2 = new Laptop(2.5, 8, 250, false, 13);
Laptop l3 = new Laptop(3.0, 16, 250, true, 15);
Fridge f1 = new Fridge(15.6, true, "Gray");
Fridge f2 = new Fridge(10.5, false, "White");
Fridge f3 = new Fridge(23, true, "Stainless Steel");
obj[0] = d1.toString();
obj[1] = d2.toString();
obj[2] = d3.toString();
obj[3] = l1.LaptoString();
obj[4] = l2.LaptoString();
obj[5] = l3.LaptoString();
obj[6] = f1.FridgetoString();
obj[7] = f2.FridgetoString();
obj[8] = f3.FridgetoString();
public void printStock(){
for (int i = 0; i < 9; i++){ \\ here it says ; expected
System.out.println(obj[i]);
}
}
}
}
Upvotes: 0
Views: 40
Reputation: 161
The issue here is not with the for loop, it is that you are defining a new function inside the constructor
If you remove the 'public void printStock() {' and its closing brace and leave the for loop as is, it should work as you expect.
If you need the printStock function it will have to be defined outside the constructor, but it will not have access to your object array which is defined in the local scope of the constructor.
Upvotes: 2