Hi guys, please let me start by saying that I am completely new to Java and that this is the first program i've attempted to write since starting to learn it on tuesday.
Basically i need to write a program that will take resource names from a html document as a string and store them within an array.
here is an example of a resource name <@dynamichtml resource_NAME @> basically in this line of data all i want is the resource_NAME so. what i have done is write a program that will take the values of <@dynamichtml and @> and then capture whatever data is stored between them in a string.
The instances of each resource are always on one line however not every line in the html file is a resource, on some lines its just html or comments etc.
So to do this I read in the html file via a buffer and then search for the string line i need. If the line is true then i store it within an array list otherwise i continue the loop.
once the document has finished being read i then sort and print the array. However although my program is compiling without any errors i am not getting any output. If someone could please solve my problem for me they would literally make my week. This program is driving me crazy.
kind and greatful regards N
| Code: |
/**import java librarys*/
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Collections;
import java.util.List;
/**class to find string*/
public class Find {
BufferedReader br = null;
/**method to read html file line by line and return string to tore in array*/
public Find() throws java.io.IOException {
try {
/**read in file*/
br = new BufferedReader(new FileReader("C:/Documents and Settings/Kieren McDonald/Desktop/Nick/Java/my.html"));
/**declaring string identifiers for beginning and end of string aswel as one other line string to store the data between*/
String line = null;
String beg ="<@dynamichtml ";
String end ="@>";
ArrayList<String> arrayList = new ArrayList<String>();
/**while line is not equal to null then find 3 string values*/
while ((line = br.readLine()) != null) {
line = find(beg,end,line);
/**if line is not equal to null store line in array list adding a resource name*/
if (line != null){
arrayList.add(line);}
}
/**sort and print array before calling printOrderedList method*/
Collections.sort(arrayList);
System.out.println(arrayList);
printOrderedList(arrayList);
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
finally {
br.close();
}
}
/**method to find the value between the beginning and end of a string*/
public static String find(String beg, String end, String line) {
/**match pattern to store string between strings and match to line*/
Pattern p = Pattern.compile(beg + "(.*)" + end);
Matcher m = p.matcher(line);
return m.find() ? line.substring(m.start(1), m.end(1)) : null;
}
/**returns contents of an array before sorting and printing it out*/
public static void printOrderedList(List<String> l){
Collections.sort(l); //list sorted
System.out.println(l); //list printed
}
/**main method to find the resource name*/
public static void main(String[] args) throws IOException {
Find findResources = new Find();
}
}
|