Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array index Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array index" Watch "Array index" New topic
Author

Array index

Bob Ryan
Greenhorn

Joined: Oct 14, 2004
Posts: 6
Hi all
When using this syntax in a method I get an error.(Line 2)
incompatible types

1 { if (......)
2 coll[wordsAdded] = word; // word is a String, wordsAdded is int
3 wordsAdded++;
4 return true;
5 }

I intended to make the array equal the String that was passed to it from the calling function, at the indexed position wordsAdded.
Could you please steer me in the right direction.
Thanks
[ October 14, 2004: Message edited by: Bob Ryan ]
[ October 14, 2004: Message edited by: Bob Ryan ]
Poonam Advani
Ranch Hand

Joined: Aug 25, 2004
Posts: 38
whats the exact error u r getting there ? and whats the data type of coll[] ?
Bob Ryan
Greenhorn

Joined: Oct 14, 2004
Posts: 6
Hi Shruti
The error is incompatible types
coll is an array of a Class
I can use coll[i] = null & it compiles in the same class in a different method.
So I can't see the difference in my other code.
coll[whatEverICallMyInt] = myStringName;

Regards
Bob
Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
Hi bob,

This is obvious that the error will be incompatible type. Becoz on the right hand side the object is an instance of String whereas on the left hand side the object is an instance of different class.

Arnab
Bob Ryan
Greenhorn

Joined: Oct 14, 2004
Posts: 6
Thanks For the reply
I don't quiet get your answer
Here is some of my code.
Can you give me some guidence of what it should be in order to compile.

class TestCollection
{
private Test[] coll;
private int whatEverICallMyInt;
}

public boolean newTest(String myStringName) // Create a new test
{
if(.....)
coll[whatEverICallMyInt] = myStringName; /* Shouldn't this move myStringName into the coll array at the indexed position whatEverICallMyInt or have I got it wrong???*/
return true;
else
return false;
} //End of Method
[ October 14, 2004: Message edited by: Bob Ryan ]
Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
Hi bob,

What u are expecting wont occur. That is the array wont point to the string.
How could asign a string to a class "Test". This is obviously not possible.
but if u do a little bit manipulation then it will assign. See the modification below .....

class TestCollection
{
private Object[] coll; //------ change is here, the name Test is //-----replaced by Object

private int whatEverICallMyInt;
}

public boolean newTest(myStringName) // Create a new test
{
if(.....)
coll[whatEverICallMyInt] = myStringName; /* Shouldn't this move myStringName into the coll array at the indexed position whatEverICallMyInt or have I got it wrong???*/
return true;
else
return false;
} //End of Method


Arnab
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you want variable coll to reference an array of references to Strings you should define coll as:

String[] coll = new String[42]; // 42 is the universal array size

So you can do:

coll[wordsAdded] = word; // to put a reference to a String into the array

or conversely;

word = coll[wordsAdded]; // to set a reference to a String to an element of the array
[ October 14, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Bob Ryan
Greenhorn

Joined: Oct 14, 2004
Posts: 6
Hi Again
Thanks for you help.
I discovered my error after thinking about the info provided by you all. It was in my method declaration, instead of string it should have been a class

public boolean newTest( String myStringName)
should have been .....( Class myName)
coll[whatEverICallMyInt] = myName;
return true;
else
return false;
} //End of Method


Regards
Bob
[ October 14, 2004: Message edited by: Bob Ryan ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array index
 
Similar Threads
passing class? PLEASE HELP
JMS message
Is Illinois still the Silicon Prairie?
Thread Priority
ebcdic to ascii