Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem reading in data to populate table

 
Mr.David Shapiro
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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 java.net.URL to open and read the file called monitor.out that has in it lines like:

TR
TDstuff/TD
TDmore/TD
TDand more/TD
/TR

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 */
java.net.URL url =config.getServletContext().getResource("/monitor.out");
BufferedReader br =new BufferedReader(new InputStreamReader(url.openStream()));
%
TABLE width='100%' border='0' cellspacing='0' cellpadding='0'
TR
TH /TH
THLoginable/TH
THPingable/TH
THApplication/TH
/TR %
while (br.readLine() != null) {
line = br.readLine();
if (line != null) { // not sure why but I get a null line
out.println(line);
}
} %
/TABLE %
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.
 
David Dong
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is a very common mistake!! Don't read twice.. make sure you call readLine once..
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic