Objects in a FOR loop

Can anyone see why this loop won’t work?:

for (int i = 0; i < name.length; i++)
{
System.out.println(instrument[i].getName());

	}
  • Where “name” is an array with 12 elements,
  • Where “instrument” is an object (there are 12 objects each named instrument1, instrument2, instrument3 etc).
  • Each instrument object has a “getName” method which returns it’s name attribute.

I was hoping this would print out 12 lines - each line being the “name” of each instrument, but instead, on compilation i get the following error:

error75: variable instrument not found in class

But of course, instrument isn’t a varbiable, it’s an object!

Any help would really really be appreciated :slight_smile:

whoops i missed the "i"out there, the actual code is:

for (int i = 0; i < name.length; i++)
{
System.out.println(instrument[i].getName());

}

anyone know why this doesn’t work? I explained what everything is in my original post above.

Ok, i didn’t miss it out the first time - this board seems to be reading my [ i ] as an italics marker. Ok, here’s the code - please ignore the spaces in the [ i ]:
whoops i missed the "i"out there, the actual code is:

for (int i = 0; i < name.length; i++)
{
System.out.println(instrument[ i ].getName());

}

anyone know why this doesn’t work? I explained what everything is in my original post above.

I want to know about loop “for(; ;)”.what is doing this loop.Here 2 ;; in braces

        for(; ;)

{
some code here
}

Hi sahil,

It acts as an infinite loop.

Martyn

for ( expression1 ; expression2 ; expression3 ){

// Body of the loop.

}

Although expression1, expression2 and expression3 are optional (can be empty) the for loop cosiders expression2 as ‘true’ if it is not present.

public class array2 {

 public static void main( String args [ ] ) {

	ins instrument [ ] = new ins [ 3 ] ;
	int name [ ] = new int[instrument.length];

	instrument [ 0 ] = new ins("Ins1");
	instrument [ 1 ] = new ins("Ins2");
	instrument [ 2 ] = new ins("Ins3");

	for (int i=0; i<name.length; i++){
		System.out.println(instrument [ i ].getName());

	} 
 
  }

}

class ins{

String	name;
public ins( String n){
	this.name=n;
}
public String getName(){
	return name;
}

}

Look at this code code… same as yours. It works fine and the output is

Ins1
Ins2
Ins3

Output completed (0 sec consumed) - Normal Termination

May be you’ve gone wrong somewhere else.
(again… Please ingnore the spaces near square braces)

hi pete ,

The problem may be in either

1.SCOPE of the variable instrument

2.you may fail to INSTANSTIATION of the instrument class.

so what i suggest is ,

u do the following things ,

1.check the scope of the instrument variable, make that available as global.

2.int name [ ] = new int[instrument.length];

instrument [ 0 ] = new ins(“Ins1”);
instrument [ 1 ] = new ins(“Ins2”);
instrument [ 2 ] = new ins(“Ins3”);

do this . ie. do the

           I.array declaration..

          II.class instantiation.

and u will get ur deired result.