This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Method:  Return String Array position Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method:  Return String Array position" Watch "Method:  Return String Array position" New topic

Method: Return String Array position

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I am new to Java so please forgive me if the answer to this question is really obvious.

I am writing a method:

I receive the following error message: incompatible types
found : java.lang.String
required: int

What I wish to do is return the array position of a string.

Could someone either explain to me why the array position of the string i.e. StudentName[position] is not being returned? Alternatively could someone point me in the right direction where I can read up some more on this?

Many thanks!!
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
Your new to Java and the ranch, I will notify a sherif to move to this to the correct place. This location is for posting advertisements not questions.

Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Hi, Maureen -

In your else statement, you're trying to return an array element. I think you want to return the index of the element, not the element itself.


Otherwise, you need to change the signature to

then return a string either way. You could return a "Invalid Index" string for the if case.

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Anyone told you your great!
I've spent hours trying to work out why this wouldn't work.
The latter one did it for me. It was obvious once you pointed it out - I feel really silly. Again, many thanks!
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
You're very welcome. Glad I could help.

I know this one because I've had to fix it in my own code once or twice!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Method: Return String Array position
It's not a secret anymore!