Você está na página 1de 4

Code:

import java.io.File;

import java.io.IOException;

import java.util.Scanner;

import java.util.regex.Pattern;

import java.util.HashMap;

// Question 3

public class ArticleIndexer {

public static void main(String[] args) throws IOException {

//System.out.print( "Enter the filename: " );


//Scanner scanner = new Scanner( System.in );

//String fileName = scanner.nextLine();

// getting a file name from the user

File inFile = new File("article2content.txt");

Scanner in = new Scanner(inFile);

String page = in.useDelimiter(Pattern.compile("\\A")).next();

int beg = -1;

for (int i = 0; i < page.length(); i++) {

if (i + "<a href=".length() <= page.length()) {

String tag = page.substring(i, i + 8);

if (tag.equals("<a href=") || tag.equals("<a


HREF="))

beg = i;

if (i + "</a>".length() <= page.length()) {

String tag = page.substring(i, i + 4);

if ((tag.equals("</a>") || tag.equals("</A>"))
&& beg != -1) {

String url = page.substring(beg, i);

beg = -1;

//System.out.println(url);

// Boolean finder for string guardian using Hashmap


String guardian = "guardian";

int urlLength = url.length();

int guardianLength = guardian.length();

int count = 0;

boolean foundIt = false;

HashMap<Integer, String> map = new HashMap<Integer, String>();

for (int i1 = 0;i1 <= (urlLength - guardianLength);i1++) {

if (url.regionMatches(i1, guardian, 0, guardianLength))

foundIt = true; count++;

String k = url;

map.put(count, url);

for(int key: map.keySet()){

//System.out.println(key);

System.out.println(k + " " + count );

break;

in.close();
}

The Output I am getting is:


<a
href="https://witness.theguardian.com/assignment/541c1101e4b0e32cb9c53a41/1212840"
>degenerated into gunfire and rioting 1

Você também pode gostar