0

I need to use an indexof method but terminal is telling me that it needs a variable but it gets a value. Can you guys explain how I fix this?

This is what I came up with by myself.

char a = 'a';
    if (s.indexOf(a, s.length()) == 61)
        System.out.println(" Your string contains the letter 'a' at index position: " + s.indexOf(97));
    else
        System.out.println(" Your string does not contain the letter 'a'");
2
  • Can you specify the language you are using? Commented Jul 8, 2014 at 3:53
  • The tags you added are all irrelevant. Looks like Java to me. Commented Jul 8, 2014 at 3:54

1 Answer 1

2

If you are using Java. Method indexOf return the first position in string s that contains the string you pass in the method. It returns -1 if the string is not found.

if (s.indexOf("a") >= 0)
        System.out.println(" Your string contains the letter 'a' at index position: " + s.indexOf("a"));
    else
        System.out.println(" Your string does not contain the letter 'a'");
Sign up to request clarification or add additional context in comments.

4 Comments

thanks a lot this worked but I don't understand why. Can you explain it to me?
@WeekzSO If it worked, you should accept the answers. It will help others too who might face the same problem.
@WeekzSO I added a link to the java API and also a brief explanation :)
@GPRathour i can't accept the answer for another 6 minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.