aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method:  Return String Array position" Watch "Method:  Return String Array position" New topic
Author

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:
GetDynamicArray.java:34: 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
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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.

Eric
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
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.

Try:


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: 804
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method: Return String Array position
 
Similar Threads
binary search method
StringTokenizer
hashing problem
JOptionPane and ArraySize not known
problem with hashCode()