• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

explanation

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.regex.*;

public class FindAt{
private static Pattern pattern;
private static Matcher matcher;
public static void main(String argv[]){
new FindAt();
}
FindAt(){

pattern = Pattern.compile("@");
matcher = pattern.matcher("@someisp.com");
if(matcher.find()){
System.out.println("found @");
}
pattern = Pattern.compile(".@.");
matcher = pattern.matcher("@someisp.com");
if(matcher.find()){
System.out.println("email found");
}


}
}

can some one please help me with this code, explain me each step
from FindAt()
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anamika i am trying to explain the following program.....
as below:





In the main method you can see constructor is being called
FindAt();

but just before that two static variables have been declared
of type Pattern and Matcher(just as we declare variables of type
Integer etc..)

private static Pattern pattern;
private static Matcher matcher;

...Than as the contructor gets called


pattern = Pattern.compile("@");
matcher = pattern.matcher("@someisp.com");

the variable pattern can hold a pattern type variable
so using the class Pattern we call compile method on it.........

Pattern.compile("@")

now since pattern has a pattern in it now we
so now we are calling pattern.matcher () method
which will return a matcher,so we can assign it to
a matcher

matcher=pattern.matcher("");

Later an if condition is being used if it returns true

than System.out.println("")gets executed

If you want to know about Pattern and Matcher types
than please refer to java API

and specifically below shown package
import java.util.regex.*;



I hope this helps...................

Preparing SCJP 1.5
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic