Web Data Management (i)

Source: Internet
Author: User

Today on the Web data management, the teacher arranged a simple exercise, the topic is as follows:

  Question : Consider the following documents:
D1 = I like to watch the sun set with my friend.
D2 = The best Places to Watch the Sunset.
D3 = My friend watches the sun come up.

  Task : Write a program which can output the document ID given by an input word.

  Problem-solving ideas (1):

1. Use the Java Constains () method of the String class, where implementation code is not affixed.

Problem-solving ideas (2):

1. Splitting a string into an array of strings

2. Determine if the string array contains the given key word.

public class Exercise1 {public static void main (string[] args) {//1. Convert to an array StringBuilder D1 = new StringBuilder ("I like to Watch the sun set with my friend. "); StringBuilder D2 = new StringBuilder ("The best Places to Watch the Sunset."); StringBuilder d3 = new StringBuilder ("My friend watches the sun come up."); /delete The most later point, split string string[] SD1 = D1.deletecharat (D1.length ()-1). ToString (). Split (""); string[] Sd2 = D2.deletecharat (D2.length ()-1). ToString (). Split (""); string[] sd3 = D3.deletecharat (D3.length ()-1). ToString (). Split ("");//2. Enter the keyword scanner scanner = new Scanner (system.in); System.out.print ("Input key word:"); String Word = Scanner.next (), Boolean isSd1 = Search (SD1, Word), Boolean isSd2 = Search (SD2, Word), Boolean isSd3 = Search (SD 3, Word);//3. Traverse looking for if (isSd1 | | isSd2 | | isSd3) {System.out.print ("the key word" + word + "constains in"); if (isSd1) {System.out.print ("D1");} if (ISSD2) {System.out.print ("D2");} if (isSd3) {System.out.print ("D3");}} else {System.out.println ("notFound ");}} private static Boolean search (string[] str, string word) {Boolean flag = False;for (string string:str) {//Do not take case equality if ( String.equalsignorecase (Word) {return true;}} return flag;}}

problem-solving ideas (3):

1. Use Java Regular Expressions (this is the Reg code the author has found on the Internet)

Import Java.util.scanner;import Java.util.regex.pattern;public class Exercise1 {public static void main (string[] args) { String D1 = "I like to watch the sun set with my friend."; String D2 = "The best Places to Watch the Sunset."; String d3 = "My friend watches the sun come up."; /1. Get input, can be multiple words, space split scanner scanner = new Scanner (system.in); System.out.print ("Input key word:"); StringBuffer word = new StringBuffer (Scanner.nextline ()); Scanner.close ();/** * Constructs regular expressions, ignoring case *. * (keyword1|keyword2| ...). * */string words = word.tostring (). Replace (', ' | ');        StringBuilder reg = new StringBuilder (". * ("). append (words). Append ("). *");                  Pattern pattern = Pattern.compile (reg.tostring (), pattern.case_insensitive); Boolean isSd1 = Pattern.matcher (D1). Matches (); Boolean isSd2 = Pattern.matcher (D2). Matches (); Boolean isSd3 =  Pattern.matcher (D3). matches (); if (isSd1 | | isSd2 | | isSd3) {System.out.print ("the key word" + word + "constains in"); if (ISSD1) {System.out.print ("D1");} if (ISSD2) {System.out.print ("D2");} if (isSd3) {System.out.print ("D3");}} else {System.out.println ("not Found");}}}

    

Web Data Management (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.