• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
 
There is no "i" in denial. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic