Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Parsing a String (BufferReader)

Posted on 2004-04-21
5
Medium Priority
?
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

721 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