| Author |
Message |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 06/04/2010 07:27:12
|
SwissR
Newbie
Joined: 06/04/2010 07:21:09
Messages: 4
Offline
|
Hi everyone, im new to Java and am writing a program which I've almost finished, basically the initial program needed to read in a html file line by line and then extract a certain string between two other strings in order to return the value i wanted.
With each instance returned I needed to store them in an array which was then sorted and printed out alphabetically. Now i need to print the contents of my array onto a new document but im having a bit of trouble. I've created a method to do this but am really stuck, please can anyone help me out.
regards Nick
[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;
import java.io.PrintWriter;
/** 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/timelinemanager_resource.htm"));
/**
* declaring string identifiers for beginning and end of string
* aswel as one other line string to store the data between
*/
String resourceline = 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 ((resourceline = br.readLine()) != null) {
resourceline = find(beg, end, resourceline);
/**
* if line is not equal to null store line in array list adding
* a resource name
*/
if (resourceline != null) {
resourceline= resourceline.trim();
arrayList.add(resourceline);
}
}
sortAndPrint(arrayList);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} finally {
br.close();
}
}
/** method to sort and print the array collection */
private void sortAndPrint(List<String> results) {
Collections.sort(results,String.CASE_INSENSITIVE_ORDER);
for (String resourceline : results)
System.out.println(resourceline);
}
/** method to find the value between the beginning and end of a string */
public String find(String beg, String end, String resourceline) {
/** match pattern to store string between strings and match to line */
Pattern p = Pattern.compile(beg + "(.*)" + end);
Matcher m = p.matcher(resourceline);
return m.find() ? resourceline.substring(m.start(1), m.end(1)) : null;
}
/** method to create a new file to print arrayList collection.*/
private void writeResults(ArrayList<String> arrayList, String filename){
for(String resourceline: arrayList)
outputStream.println(resourceline);
}
}
/** main method to find the resource name calling */
public static void main(String[] args) throws IOException {
Find findResources = new Find();
}
}
[/CODE]
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 06/04/2010 08:01:54
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
The code as posted doesn't compile but it looks like it should be ok. I don't have the file you're parsing so it's difficult to debug. Where is it not working?
|
Thanks for using the forums at hotjoe.com |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 06/04/2010 08:20:01
|
SwissR
Newbie
Joined: 06/04/2010 07:21:09
Messages: 4
Offline
|
The code doesnt compile purely because of the writeResults method, comment that out and it will work.
Basically I need to get that method working to print my collection line by line into the new document. i'll attatch the html file.
Basically the html file contains resource references which are always between the strings "<@dynamichtml " and "@>" the idea is that the resourceline string returns the value between the two and stores it in an array. once all these instances are collected and the buffered reader has read the document the array gets sorted and printed out.
Basically all i need to do now is write and call the method to write the contents of the array line by line into a new document.
regards Nick
| Filename |
timelinemanager_resource.htm |
Download
|
| Description |
|
| Filesize |
2 Kbytes
|
| Downloaded: |
38 time(s) |
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 06/04/2010 08:30:24
|
SwissR
Newbie
Joined: 06/04/2010 07:21:09
Messages: 4
Offline
|
Apologies i just re-read my last message and realised i'd typed basically about 70 times lol
regards Nick
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 06/04/2010 08:40:39
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
Everything seems to work as is except for writeResults. How about a simple:
|
Thanks for using the forums at hotjoe.com |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 06/04/2010 08:58:13
|
SwissR
Newbie
Joined: 06/04/2010 07:21:09
Messages: 4
Offline
|
I'll give that a go, thanks
|
|
|
 |
|
|