File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Linux / UNIX and the fly likes Problem reading in data to populate table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Problem reading in data to populate table" Watch "Problem reading in data to populate table" New topic

Problem reading in data to populate table

Mr.David Shapiro
Ranch Hand

Joined: Apr 18, 2005
Posts: 53

I am having a problem with some code where I read in a file with data to create a table, but it drops some of the lines or something (e.g., lose a table TR or a TD or whatever). I used to open and read the file called monitor.out that has in it lines like:

TDand more/TD

Note: Removed greater than/less than signs around html code so I can post here

It will drop the first TR and throws it all off. Am I using the right method to read in a file to populate a table? Is there something else going on?

/* file does exist so display data */ url =config.getServletContext().getResource("/monitor.out");
BufferedReader br =new BufferedReader(new InputStreamReader(url.openStream()));
TABLE width='100%' border='0' cellspacing='0' cellpadding='0'
/TR %
while (br.readLine() != null) {
line = br.readLine();
if (line != null) { // not sure why but I get a null line
} %
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

Here's your code with some explanatory comments

You're discarding every second line, because you read two lines for each loop iteration! The right way to write this loop:

Some people don't like that complicated loop condition; an alternative, then, is to write:

Finally, I don't know why you posted this in our Linux forum; we have other forums, like JSP or even Java in General, that would have been more appropriate.

[Jess in Action][AskingGoodQuestions]
David Dong
Ranch Hand

Joined: Aug 15, 2004
Posts: 58
that is a very common mistake!! Don't read twice.. make sure you call readLine once..
I agree. Here's the link:
subject: Problem reading in data to populate table
It's not a secret anymore!