Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method: Return String Array position

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic