Reputation: 3725
Please see the attached image:
1) I downloaded a new library here: (http://www.java2s.com/Code/Jar/ABC/Downloadcommonslang24jar.htm)
2) In Eclipse, I right-clicked on 'Referenced Libraries' > Build Path > Configure Build Path > Add External JARs and added 'commons-lang-2.4.jar'
3) I've added import org.apache.commons.lang.*
at the top of my class.
4) I entered a method from that class indexOfAny()
and get the following error: 'The method indexOfAny() is undefined for the type String.'
What step(s) am I missing? Which steps that I've taken are unnecessary? I need to be able to use this method.
P.S. Pls ignore the rest of the code.
Upvotes: 0
Views: 1947
Reputation: 20371
You're not using it right, you're trying to invoke the method indexOfAny()
on a java.lang.String
object - this method is not part of that class. You need to call these methods statically on org.apache.commons.lang.StringUtils
- something of the form StringUtils.XXX()
The Commons Library doesn't augment existing classes (in any case, java.lang.String
is final
). According to the documentation, your call should be something that looks like:
StringUtils.indexOfAny(quantityInForPriceBandPopUp[i], ['z'])
or using one of the overloaded versions.
Update
Is importing the package necessary?
This article and the Java package trail should help with all the details of packages and imports. But some of the basic things to understand are :
Packages are the namespacing mechanism that Java uses - this allows you and I both to write a Utils
class while avoiding a collision because of the same name. For example, java.sql.Date
and java.util.Date
- two Date
classes can exist and be used because they're in different packages. It might help to envision packages and their sub-packages as a hierarchy of folders.
Imports are a convenience feature that lets you reference classes by their simple name (Math
or String
) instead of their fully qualified name (FQN) (java.lang.Math
or java.lang.String
) every single time you want to use it, which gets painful and clutters up your code, making it much less readable.
Imports don't add anything to your code or make it less efficient since the imported packages aren't linked to your code or anything like that - as mentioned above, it's simply a way to avoid having to use the FQN all the time.
So to answer your question, no, the import is not necessary but then you'd have to use org.apache.commons.lang.StringUtils
every single time instead of just being able to use StringUtils
. So while not necessary, it's usually convenient both for yourself and anyone else who's going to try to read your code.
Upvotes: 2
Reputation: 26809
Because in documentation you have:
static int indexOfAny(String str, char[] searchChars)
Search a String to find the first index of any character in the given set of characters.
Upvotes: 2
Reputation: 8204
indexOfAny() is NOT a method on a String object.
Maybe you meant to write StringUtils.indexOfAny(...)
Upvotes: 4
Reputation: 18276
You are trying to use these methods on Strings, you should read the documentation of the library you want to use.
Upvotes: 2