Solved

Parsing a String (BufferReader)

Posted on 2004-04-21
5
420 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 150 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now