Thursday, 1 September 2016

CORE JAVA LAB@HOME 8

Question-1 Accept the three letter an the display the matching lettter the user.

Answer:-

import java . util . Scanner;

import java . util . regex . Matcher;

imort java  . util . regex . Pattern;


 public class TestRegx {


 public static void main (String [] args) {

 Scanner sc = new Scanner (System . in);

 String input;


 System.out..print ("Enter the string: ");

 input = sc . next ();

 Pattern myPattern = Pattern . compile (" [abc] at");

 Matcher myMatcher = myPattern . matcher (input);

 boolen  myBoolen  = myMatcher . matches ();

 if (myBoolen) {

      System.out.println ("Expression is Matched");

    } else {

      System.out.println ("Expression Not Matched");

    }

  }


}


Question-2 Search the text file from the directory.

Answer:-

Create a simple applications that will loop through a text file(gettys.html) and search for

text by using regular expresions. If the desired text found on a line, print out the line number

and the line text.

9 <h4>Abrahan Lincoln</h4>

10 <h4>Thrusday, November 19, 1863</h4>


Tasks

 The gettys.html file is located in the root of the project folder. To examine the file,

with the project open, click the file tab. Double-click the file to open it and exmaine its

content.

   1. Edit teh FindText.java file.

   2. Create a pattern and a Matcher field.

   3. Generate a Matcher based on the supplied Pattern object.

   4. Search each line for the pattern supplied.

   5. Print the line numver and the line that has matching text.

   6. Run the FindText.java file and search for these patterns.

              All lines that contain <h4>

              All the ines that contain the word "to" (For example, line 17 should

              not be selected.)

              All the lines that start with 4 spaces'

              Lines that bedin with "<p" or "<d"

              Lines that only contain HTML closing tags( for example, "</div>")

                 

Open the StringPractice02 project and make the followng changes. Please note that the

code to read a file has been supplied for you.

    1. Edit the FindText.java file.

    2. Create fields for a Pattern and a Matcher object

       private Pattern pattern;

       private Matcher m;

    3. Outside the search loop, create and initialize your pattern object.

       pattern = pattern.compile("<h4>");

    4. Inside the search loop, generate a Matcher based on the supplied Pattern

       object.

      m = pattern.matcher(line);

    5. Inside the search loop, search each line for the pattern supplied. print the line

       number and the line that has matching text.

       if (m.find() )  {

           System.out.println(" " + " "+ line);

       }

   6. Run the FindText.java file and search for these patterns.

              All the lines that contain <h4>

              pattern = Pattern.compile("<h4>");

            

             All the lines that contain the word "to" (For example, line 17 should

             not be selected.)

    

             pattern = Pattern.compile("\\bto\\b"0;

       

             All the lines that start with 4 spaces

            

            pattern = Pattern.compile("^\\s{4}");


            Lines that begin with "<p" or "<d"


            pattern = Pattern.compile("^<[p|d]");


            Lines that only contain HTML closin tags(For example, "</div>")


Question-3 Find the URL

Answer:-

import java . util . Scanner;

import java . util . regex . Matcher;

import java . util . regex . Pattern;


class UrlChecker {


 public static void main (String [] args) {

   pattern Mypattern = Pattern . compile ("^ ( (https ? |

 ftp) : // | (www | ftp) \\ .) [ a-zO-9-] + [a-z] + ) + ( [/ ?] . *) ?$" );

      Scanner input = new Scanner (System . in);

      System.out.println ("Enter the Url  to be checked:

  ");

      String name = input . nextLine ();

      Matcher Mymatcher = Mypattern . matcher (name);

      Boolen Myboolen = Mymatcher . matches ();

      if (Myboolen == true) {

          System.out.println ("Url is correct");

       } else {

          System.out.println ("Url is incorrect");

     }

  }


}

No comments:

Post a Comment