Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Parsing a String (BufferReader)

Posted on 2004-04-21
5
Medium Priority
?
429 Views
Last Modified: 2012-06-27
Hi

I am using the following code and a bit more to download a html file from a website and it is then turned into a string.

URLConnection connection = javacodingURL.openConnection();
BufferedReader br = new BufferedReader(newInputStreamReader(connection.getInputStream()));
String line = "";
while ((line = br.readLine()) != null)
System.out.println(line);
br.close();

My code works, and I get a ouput of the html file. Though what I want is specfic text but I don't want to use the HTMLEditor from java as it causes too many problems. The html file has some code at the start which I want to ignore and it then follows a pattern:

It starts with <p align="center">............(code I need)......<hr color="#87A9DC">. Then again it starts with  <p align="center">............(code I need)......<hr color="#87A9DC"> (goes on in that format till then end)

My question is how would I get the all the html code between the p align code and hr color and place the data individualy into a arraylist?

Thanks for any help
Akbar
0
Comment
Question by:akzah
  • 4
5 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10877631
Method:
1) Search your string for "<p align="center">"
2) From that place search for "<hr color="
3) Get that in between.

The function String.indexOf() will be useful:

int indexOf(String str, int fromIndex)
          Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 600 total points
ID: 10877747
String yourString = ... // gor from your file

List arrayList = new ArrayList();

int i1 = 0;
int i2 = 0;
int start = 0;
i1 = yourString.indexOf("<p align", 0);
start = yourString.indexOf(">", i1);
i2 = yourString.indexOf("<hr color", i1);

Visually:

xwvqsdf <p align="center">My text fgqsfgsd dsqg sdgsdf sdfgsd sdfsg End of my text<hr color=#xxxxxx>bla bla bla
             |                         |                                                                                 |
            i1                         start                                                                            i2

arrayList.add( yourString.substring(start, i2) );  // adding "My text fgqsfgsd dsqg sdgsdf sdfgsd sdfsg End of my text"

You can restart for another loop when you perform:

yourString = yourString.substring(i2);
0
 

Author Comment

by:akzah
ID: 10877766
that method sounds good, though how would I get that to work on the BufferedReader br. How would I turn that into a string?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10877835
StringBuffer buff = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null)
     buff.append(line);

String allTheText = buff.toString();
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10878603
Thanks for accepting akzah
:)
That keeps us answering your future questions too.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question