Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

like image 814
Bas Jansen Avatar asked Jan 09 '12 15:01

Bas Jansen


3 Answers

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.

like image 150
pcalcao Avatar answered Nov 16 '22 16:11

pcalcao


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.

like image 30
JB Nizet Avatar answered Nov 16 '22 16:11

JB Nizet


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.

like image 20
ruakh Avatar answered Nov 16 '22 17:11

ruakh