Results 1 to 4 of 4
  1. #1
    DrMath is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Cool How does the '*' work in the code?

    import java.util.regex.*;
    class Regex2 {
    public static void main(String[] args) {
    Pattern p = Pattern.compile(args[0]);
    Matcher m = p.matcher(args[1]);
    boolean b = false;
    while(b = m.find()) {
    System.out.print(m.start() + m.group());
    }
    }
    }
    And the command line:
    java Regex2 "\d*" ab34ef
    What is the result?
    A. 234
    B. 334
    C. 2334
    D. 0123456
    E. 01234456
    F. 12334567
    G. Compilation fails

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Which one, the one in the import statement, or the command-line input?

    In the import statement, it's a "star import" - it imports ALL classes in that package. However, it can get you into trouble if two packages have the same class. I saw someone do this by importing java.awt.* and java.util.*. Both have a "List" class, and you can't import two classes with the same name, because the compiler can't figure out which you intended. Also, when working with an IDE (e.g. Eclipse, NetBeans), I've heard star imports can cause problems. I don't use them, I let the IDE import the necessary classes for me, so I'm not sure what kind of problems, or how they occur.

    As for the star in the command-line, when it's compiled in the regex, it means "0 or more (of the previous element)". So, in the code above, it means 0 or more digits.
    Last edited by CodesAway; 10-28-2009 at 08:05 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    DrMath is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default

    Commandline...
    thanks. :)

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Welcome.

    If you're unfamiliar with regex syntax, check out regular-expressions.info/, which has an amazing regular expression tutorial.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Replies: 2
    Last Post: 07-29-2009, 01:23 PM
  2. Replies: 6
    Last Post: 02-06-2009, 09:05 PM
  3. how does this code work...explain me the execution please...
    By vital parsley in forum New To Java
    Replies: 3
    Last Post: 07-25-2008, 05:50 AM
  4. the code dosent work
    By pcman in forum Java 2D
    Replies: 1
    Last Post: 03-20-2008, 09:20 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •