| Author |
Problem reading in data to populate table
|
Mr.David Shapiro
Ranch Hand
Joined: Apr 18, 2005
Posts: 51
|
|
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
Joined: Jul 08, 2003
Posts: 24057
|
|
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.
|
[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..
|
 |
 |
|
|
subject: Problem reading in data to populate table
|
|
|