简体   繁体   中英

import java.util.regex fails

I get an error when attempting to import the java.util.regex (specifically added the line to figure out that the error is in the import as I previously only had import java.util.*).

find_glycopeps.java:5: cannot find symbol
symbol  : class regex
location: package java.util
import java.util.regex; // Should be redundant...
<some more messages about not recognising Pattern and Matcher, which are classes of the regex package>

As far as I am aware, the regex is a 'core' library. I am assuming that since import java.io.* works that the native method of keeping track of where libraries are should be working so I am quite puzzled how this has occured.

PS: I have to note that I have tested some java compilers over the weekend to find 1 that I like and re-installed a 'clean' openjdk-6 this morning, this is probably where the problems originate from but not sure how to proceed.

Cheers

EDIT (SOLVED): .. I will definitely go hide in shame now, thank you all for pointing out the truly silly mistake .

Your import is defined wrong.

You'll either need to provide explicit imports of each class, as so:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

Or do

import java.util.regex.*;

You're trying to import a package, you need the * meta-character for that.

If you read the message the compiler gives you, it says it can't find Class regex.

You can't import a package. You import a class, or all classes in a package:

import java.util.regex.*;

Packages are organized in a tree, but import is not recursive. Importing java.util.* only imports classes in java.util , but not classes from sub-packages.

You need to write either:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

or else:

import java.util.regex.*;

You can't just import java.util.regex , without the asterisk, since that's a package; it would be like importing java.io .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM