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.

- And obviously there's not really any spaces in the [ i ] - i've had to do that to prevent going into italics on this board!

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 :)

[888 byte] By [peteL] at [2007-9-30 21:53:53]
# 1

Either you didn't declare a variable named instrument, or you declared in a scope not accessible from the code you posted. You'll have to post more code.

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/features.jsp#Formatting]Formatting Help[/url] on the message entry page. It makes it much easier to read.

jverd at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 2

> But of course, instrument isn't a varbiable, it's an

> object!

No. If instrument exists, it's a variable. Depending on how you declared it, it could be a variable of a reference type that refers to an object.

Understanding the distinction between variables and their values, and between references and the objects they refer to, is very important to understanding Java.

jverd at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 3

is this something like what you're trying to do

class Testing

{

public Testing()

{

Instrument instrument1 = new Instrument("Guitar");

Instrument instrument2 = new Instrument("Trumpet");

Instrument instrument3 = new Instrument("Violin");

Instrument[] name = {instrument1,instrument2,instrument3};

for(int x = 0; x < name.length; x++)

{

System.out.println(name[x].getName());

}

}

public static void main(String[] args){new Testing();}

}

class Instrument

{

private String name;

public Instrument(String n){name = n;}

public String getName(){return name;}

}

Michael_Dunn at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 4

thanks for replying so swiftly. Basically, i'm really really new to this so i think the best thing for me to do would be to post to whole program so far asi don't know if i've declared instrument wrongly somewhere else. In the below code i've got 2 classes within the same program:

public class instrumentArraySearch03

{

public static void main(String[] args)

{

String searchCriteria;// variables

int searchChoice;

String againYesNo;

String [] name = new String[13];// create name array

String [] type = new String[13];// create type array

String [] price = new String[13];// create price array

name[0] = "fender";

name[1] = "gibson";

name[2] = "squire";

name[3] = "casio";

name[4] = "korg";

name[5] = "shure";

name[6] = "akg";

name[7] = "mcscott";

name[8] = "pearl";

name[9] = "ludwig";

name[10] = "classical co.";

name[11] = "sax co.";

name[12] = "violin co.";

type[0] = "electric guitar";

type[1] = "acoustic guitar";

type[2] = "electric bass";

type[3] = "acosutic bass";

type[4] = "banjo";

type[5] = "violin";

type[6] = "saxophone";

type[7] = "clarinet";

type[8] = "flute";

type[9] = "bagpipes";

type[10] = "keyboard";

type[11] = "drumkit";

type[12] = "microphone";

price[0] = "49";

price[1] = "59";

price[2] = "99";

price[3] = "149";

price[4] = "199";

price[5] = "249";

price[6] = "300";

price[7] = "350";

price[8] = "399";

price[9] = "450";

price[10] = "599";

price[11] = "799";

price[12] = "999";

// testing out whether objects work - see class at bottom of page

instrument instrument1 = new instrument(name[0], type[2], price[3]);

instrument instrument2 = new instrument(name[11], type[2], price[0]);

instrument instrument3 = new instrument(name[2], type[2], price[11]);

instrument instrument4 = new instrument(name[0], type[2], price[7]);

instrument instrument5 = new instrument(name[4], type[2], price[8]);

instrument instrument6 = new instrument(name[8], type[2], price[6]);

instrument instrument7 = new instrument(name[7], type[1], price[2]);

instrument instrument8 = new instrument(name[1], type[2], price[9]);

instrument instrument9 = new instrument(name[5], type[2], price[10]);

instrument instrument10 = new instrument(name[6], type[2], price[5]);

instrument instrument11 = new instrument(name[10], type[2], price[4]);

instrument instrument12 = new instrument(name[3], type[2], price[1]);

for (int i = 0; i < name.length; i++)

{

System.out.println("instrument name is " + instrument[i].getName());

}

} // ends main method

} // ends class

class instrument

{

private String name;

private String type;

private String price;

public instrument(String nameIn, String typeIn, String priceIn) // constructor

{

name = nameIn;

type = typeIn;

price = priceIn;

}

public String getName()

{

return name;

}

public String getType()

{

return type;

}

public String getPrice()

{

return price;

}

}

peteL at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 5
That's what I thought. Declaring instrument1 makes a reference called, literally, instrument1. It's a name. To use a for-loop, you need an array. From the looks of it, you're better off checking a tutorial than getting a cursory overview of arrays here.
Adeodatus at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 6
That's what I thought. Declaring instrument1 makes a reference called, literally, instrument1. It's a name. To use a for-loop, you need an array. From the looks of it, you're better off checking a tutorial than getting a cursory overview of arrays here.
Adeodatus at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 7

instrument1 is completely unrealted to instrument[1].

If you have variables called instrument1, instrument2, etc., there's no way to access those with "instrument" plus some index. Just make one variable, an array. Instrument[] instruments = new Instrument[size];

Also, why do you have those three different arrays for name, etc.? You don't need them.

Instrument[] instruments = new Instrument[13];

instrument[0] = new Instruement("fender", "guitar", 49);

etc.

jverd at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 8
thanks very much - i had a hunch that objects couldn't really be used in this way, and all the books/tutorials i've loooked at have used arrays. Therefore i'll probably ditch the objects and figure out how to keep the same list as arrays.cheers.
peteL at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 9

I don't know what you mean about "ditching objects." You still want an array of Instrument objects.

Like I said, instrument1, instrument2, etc. are not objects. They're just variables. Instead of having a bunch of individual variables, you want one variable, that refers to an array. The array's elements will take the place of the individual variables you're using now.

jverd at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 10
"Like I said, instrument1, instrument2, etc. are not objects. "Why arent they objects ?
Zangrela at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 11

> "Like I said, instrument1, instrument2, etc. are not

> objects. "

>

> Why arent they objects ?

Because they are variables. References to be exact. All those variables are is a holder for a value that tells the JVM where the Object it refers to is located or a null value. Thinking of them as Objects will just cause confusion.

dubwai at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 12
These variables points to an object, that齭 right ?
Zangrela at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 13

sorry - "ditching" is a bit of an english-ism i guess - and i was wrong saying that i was going to ditch objects anyway, as you've pointed out.

thanks very much for your help, i've put together a small piece of code to test out what you said and it's working great. On the offchance that you want to read it i'll paste it here:

public class instrumentArraySearch04

{

public static void main(String[] args)

{

Instrument [] instruments = new Instrument[3];

instruments[0] = new Instrument("fender", "guitar", 49);

instruments[1] = new Instrument("gibson", "guitar", 100);

instruments[2] = new Instrument("squire", "guitar", 150);

for (int i = 0; i < instruments.length; i++)

{

System.out.println("instrument is " + instruments[i].getName() + instruments[i].getType());

}

EasyIn.pause("Press enter to quit");

} // ends main method

} // ends class

class Instrument

{

private String name;

private String type;

private int price;

public Instrument(String nameIn, String typeIn, int priceIn) // constructor

{

name = nameIn;

type = typeIn;

price = priceIn;

}

public String getName()

{

return name;

}

public String getType()

{

return type;

}

public int getPrice()

{

return price;

}

}

peteL at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 14

> "Like I said, instrument1, instrument2, etc. are not

> objects. "

>

> Why arent they objects ?

>

Because they are variables. In some contexts, it's okay to lose the distinction, but you have to understand the distinction to be able to know when it's okay to ignore it.

instrument1 is a variable. It's an identifier that you place in your code to hold a particular value. It's used here to hold the value of a reference, and that reference is (basically) an address where the object is found.

One example of where the distinction is very important: Foo foo1 = new Foo("abc");

Foo foo2 = new Foo("xyz");

Foo foo3 = foo1;

foo3 = foo2;

foo3 = null;

If you understand the distinction between variables and values, and between references and the objects they refer to, then it's easy to understand that none of the assignemnts (e.g. foo3=...) change any objects.

If you think of foo3 as an object, you might be inclined to think that foo3=... is changes an object. It doesn't.

jverd at 2007-7-7 3:22:03 > top of Java-index,Security,Event Handling...
# 15
> These variables points to an object, thats right ?Yep.
jverda at 2007-7-20 0:43:19 > top of Java-index,Security,Event Handling...
# 16
> thanks very much for your help, i've put together a> small piece of code to test out what you said and it's> working great. Good. I'm glad you got it working.
jverda at 2007-7-20 0:43:19 > top of Java-index,Security,Event Handling...
# 17
"In some contexts, it's okay to lose the distinction"This is what i have done.Thankx anyway
Zangrelaa at 2007-7-20 0:43:19 > top of Java-index,Security,Event Handling...