File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array index

 
Bob Ryan
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whats the exact error u r getting there ? and whats the data type of coll[] ?
 
Bob Ryan
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bob Ryan
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic