4 Replies - 174 Views - Last Post: 06 August 2012 - 02:44 PM Rate Topic: -----

#1 kaizerr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 02-August 11

Pattern and Matcher

Posted 06 August 2012 - 02:03 PM

hello, i am testing pattern and matcher for unique no. although it seems pointless in my code but, nevertheless i just want to understand why it fails and if a solution is available, i am all ears to all mentors. thanks.

int uniqueNo = new Double( Math.random() * 100000000 ).intValue();

Pattern nPat = Pattern.compile("[0-9]{0,8}");
Matcher nM = nPat.matcher(uniqueNo);
boolean nCheck = nM.matches();



but it gives me an error says that

error: method matcher in class pattern cannot be applied to the given types;



Is This A Good Question/Topic? 0
  • +

Replies To: Pattern and Matcher

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Pattern and Matcher

Posted 06 August 2012 - 02:08 PM

Patterns and Matchers work on Strings, not ints. If you want to see whether the string representation of your int matches the given pattern, you'll need to convert your int to a String first.
Was This Post Helpful? 2
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1051
  • Posts: 2,229
  • Joined: 30-January 11

Re: Pattern and Matcher

Posted 06 August 2012 - 02:09 PM

The Pattern.matcher() method expects a CharSequence object (basically a String) as a parameter, not an int.

Easy fix is to Integer.toString() the int before you pass it into the .matcher method.

Edit - ninja'd

This post has been edited by Ryano121: 06 August 2012 - 02:10 PM

Was This Post Helpful? 1
  • +
  • -

#4 kaizerr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 02-August 11

Re: Pattern and Matcher

Posted 06 August 2012 - 02:18 PM

thank you very much for both of the enlightenments :)
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Pattern and Matcher

Posted 06 August 2012 - 02:44 PM

Oh and by the way: You shouldn't use new to create instances of Double (or the other numeric classes). Using valueOf (or for that matter just casting to Double, which will have the same effect) will be much more efficient.

That said, there really isn't a reason to use a Double here at all. Just use a primitive double and cast to int instead of calling intValue.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1