Not importing properly?

G

greg.johnston

I'm trying to use some imported library stuff, and it won't let
me...I've done

import java.lang.*

And I'm trying to do the following:
public boolean isAlNum(char c) {
return isLetterOrDigit(c);
}

I get:
error:Cannot find method "isLetterOrDigit(char)"

Does anyone have any ideas?
 
L

Lee Weiner

I'm trying to use some imported library stuff, and it won't let
me...I've done

import java.lang.*

And I'm trying to do the following:
public boolean isAlNum(char c) {
return isLetterOrDigit(c);
}

I get:
error:Cannot find method "isLetterOrDigit(char)"

1. You never have to import any of the classes in java.lang. The entire
package is imported automatically into every program.

2. You always have to tell Java what class a static method is in, unless it's
in the current class. Your line:
return isLetterOrDigit(c);
is telling the compiler to look for this method in the current class (where
this code is executing). Since isLetterOrDigit() is in the Character class,
change the line to:
return Character.isLetterOrDigit(c);

Lee Weiner
lee AT leeweiner DOT org
 
O

Oliver Wong

Lee Weiner said:
1. You never have to import any of the classes in java.lang. The entire
package is imported automatically into every program.

2. You always have to tell Java what class a static method is in, unless
it's
in the current class. Your line:
return isLetterOrDigit(c);
is telling the compiler to look for this method in the current class
(where
this code is executing). Since isLetterOrDigit() is in the Character
class,
change the line to:
return Character.isLetterOrDigit(c);

Lee Weiner
lee AT leeweiner DOT org

You can import static methods using the "static import" statement. I
haven't ever used it myself, so I'm not sure of the syntax, and don't want
to risk giving false advice. But google for it. It's probably something
like:

static import java.lang.Character;

to import all the static methods from the Character class.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top