While Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #16
    use this


    Code:
     
    <%
     
     
     
      String valString;
       try { 
     
       int recCount = 0;
    	FileReader dFile = new FileReader("w:/www/DataFile.txt");
    	   BufferedReader dataFile = new BufferedReader(dFile);
    	valString = dataFile.readLine();
    	while(valString != null){
    	 recCount++;
    	 out.println(valString);
    	 valString = dataFile.readLine();
    	}
       }
       catch (FileNotFoundException e) {
       System.err.println("FileStreamsTest: " + e);
      }
      catch(IOException iOE) {
       iOE.printStackTrace();
      }
      finally {
       dataFile.close();  
      }
       
     
    %>
    Do you want the values to be displayed in a table?

    Comment

    • Naha
      New Member
      • Feb 2007
      • 41

      #17
      yes I want it in a table, its still givin me the following error:

      HTTP Status 500 -

      --------------------------------------------------------------------------------

      type Exception report

      message

      description The server encountered an internal error () that prevented it from fulfilling this request.

      exception

      org.apache.jasp er.JasperExcept ion: Unable to compile class for JSP

      An error occurred at line: 24 in the jsp file: /website2/weather.jsp
      Generated servlet error:
      dataFile cannot be resolved


      org.apache.jasp er.servlet.JspS ervletWrapper.h andleJspExcepti on(JspServletWr apper.java:510)
      org.apache.jasp er.servlet.JspS ervletWrapper.s ervice(JspServl etWrapper.java: 375)
      org.apache.jasp er.servlet.JspS ervlet.serviceJ spFile(JspServl et.java:314)
      org.apache.jasp er.servlet.JspS ervlet.service( JspServlet.java :264)
      javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)


      root cause

      org.apache.jasp er.JasperExcept ion: Unable to compile class for JSP

      An error occurred at line: 24 in the jsp file: /website2/weather.jsp
      Generated servlet error:
      dataFile cannot be resolved


      org.apache.jasp er.compiler.Def aultErrorHandle r.javacError(De faultErrorHandl er.java:84)
      org.apache.jasp er.compiler.Err orDispatcher.ja vacError(ErrorD ispatcher.java: 328)
      org.apache.jasp er.compiler.JDT Compiler.genera teClass(JDTComp iler.java:414)
      org.apache.jasp er.compiler.Com piler.compile(C ompiler.java:29 7)
      org.apache.jasp er.compiler.Com piler.compile(C ompiler.java:27 6)
      org.apache.jasp er.compiler.Com piler.compile(C ompiler.java:26 4)
      org.apache.jasp er.JspCompilati onContext.compi le(JspCompilati onContext.java: 563)
      org.apache.jasp er.servlet.JspS ervletWrapper.s ervice(JspServl etWrapper.java: 303)
      org.apache.jasp er.servlet.JspS ervlet.serviceJ spFile(JspServl et.java:314)
      org.apache.jasp er.servlet.JspS ervlet.service( JspServlet.java :264)
      javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)


      note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.

      Code:
      <%@ page import="java.util.*,java.io.*" %>
      <%@ page import="testBean.TestBean" %>
      
      
      <html>
      <head>
      
      <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
      
      </head>
      
      <body>
      
      <hr></hr>
      
      <center>
      
      <h1> 
      
       Weather Forecast</h1>
       
       <%
       
        String valString;
         try { 
       
         int recCount = 0;
      	FileReader dFile = new FileReader("w:/www/DataFile.txt");
      	   BufferedReader dataFile = new BufferedReader(dFile);
      	valString = dataFile.readLine();
      	while(valString != null){
      	 recCount++;
      	 out.println(valString);
      	 valString = dataFile.readLine();
      	}
         }
         catch (FileNotFoundException e) {
         System.err.println("FileStreamsTest: " + e);
        }
        catch(IOException iOE) {
         iOE.printStackTrace();
        }
        finally {
         dataFile.close();  
        }
         
       
      %>
       
       <jsp:useBean id="temp"  class="TestBean" /> 
       
       <br><br><br>
      
      <a href="test.html"> Local Five day forecast </a>
      
      <br><br><br><br><br><br><br>
      <table>
      <tr><TD><IMG SRC="sunny.gif" ALT="S" BORDER=0 WIDTH=100 HEIGHT=100 ></TD><TD><h4> <%= temp.post() %> </h4></TD> </tr>
      </table>
      
      <br><br><br><br><br><br><br>
      
      <hr></hr>
      
      </body>
      </html>

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #18
        Originally posted by Naha
        yes I want it in a table, its still givin me the following error:

        HTTP Status 500 -

        --------------------------------------------------------------------------------

        type Exception report

        message

        description The server encountered an internal error () that prevented it from fulfilling this request.

        exception

        org.apache.jasp er.JasperExcept ion: Unable to compile class for JSP

        An error occurred at line: 24 in the jsp file: /website2/weather.jsp
        Generated servlet error:
        dataFile cannot be resolved


        org.apache.jasp er.servlet.JspS ervletWrapper.h andleJspExcepti on(JspServletWr apper.java:510)
        org.apache.jasp er.servlet.JspS ervletWrapper.s ervice(JspServl etWrapper.java: 375)
        org.apache.jasp er.servlet.JspS ervlet.serviceJ spFile(JspServl et.java:314)
        org.apache.jasp er.servlet.JspS ervlet.service( JspServlet.java :264)
        javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)


        root cause

        org.apache.jasp er.JasperExcept ion: Unable to compile class for JSP

        An error occurred at line: 24 in the jsp file: /website2/weather.jsp
        Generated servlet error:
        dataFile cannot be resolved


        org.apache.jasp er.compiler.Def aultErrorHandle r.javacError(De faultErrorHandl er.java:84)
        org.apache.jasp er.compiler.Err orDispatcher.ja vacError(ErrorD ispatcher.java: 328)
        org.apache.jasp er.compiler.JDT Compiler.genera teClass(JDTComp iler.java:414)
        org.apache.jasp er.compiler.Com piler.compile(C ompiler.java:29 7)
        org.apache.jasp er.compiler.Com piler.compile(C ompiler.java:27 6)
        org.apache.jasp er.compiler.Com piler.compile(C ompiler.java:26 4)
        org.apache.jasp er.JspCompilati onContext.compi le(JspCompilati onContext.java: 563)
        org.apache.jasp er.servlet.JspS ervletWrapper.s ervice(JspServl etWrapper.java: 303)
        org.apache.jasp er.servlet.JspS ervlet.serviceJ spFile(JspServl et.java:314)
        org.apache.jasp er.servlet.JspS ervlet.service( JspServlet.java :264)
        javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)


        note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.

        Code:
         
        <%@ page import="java.util.*,java.io.*" %>
        <%@ page import="testBean.TestBean" %>
         
         
        <html>
        <head>
         
        <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
         
        </head>
         
        <body>
         
        <hr></hr>
         
        <center>
         
        <h1> 
         
        Weather Forecast</h1>
         
        <%
         
        String valString;
        try { 
         
        int recCount = 0;
        	FileReader dFile = new FileReader("w:/www/DataFile.txt");
        	 BufferedReader dataFile = new BufferedReader(dFile);
        	valString = dataFile.readLine();
        	while(valString != null){
        	 recCount++;
        	 out.println(valString);
        	 valString = dataFile.readLine();
        	}
        }
        catch (FileNotFoundException e) {
        System.err.println("FileStreamsTest: " + e);
        }
        catch(IOException iOE) {
        iOE.printStackTrace();
        }
        finally {
        dataFile.close(); 
        }
         
         
        %>
         
        <jsp:useBean id="temp" class="TestBean" /> 
         
        <br><br><br>
         
        <a href="test.html"> Local Five day forecast </a>
         
        <br><br><br><br><br><br><br>
        <table>
        <tr><TD><IMG SRC="sunny.gif" ALT="S" BORDER=0 WIDTH=100 HEIGHT=100 ></TD><TD><h4> <%= temp.post() %> </h4></TD> </tr>
        </table>
         
        <br><br><br><br><br><br><br>
         
        <hr></hr>
         
        </body>
        </html>
        Just change to
        Code:
         <% 
         
          String valString;
          BufferedReader dataFile = null;
           try {
           int recCount = 0;
        	FileReader dFile = new FileReader("w:/www/DataFile.txt");
        	   dataFile = new BufferedReader(dFile);
        	valString = dataFile.readLine();
        	while(valString != null){
        	 recCount++;
        	 out.println(valString);
        	 valString = dataFile.readLine();
        	}
           }
           catch (FileNotFoundException e) {
           System.err.println("FileStreamsTest: " + e);
          }
          catch(IOException iOE) {
           iOE.printStackTrace();
          }
          finally {
           dataFile.close();
          }
        
        %>
        What do you want the table to look like?

        Comment

        • Naha
          New Member
          • Feb 2007
          • 41

          #19
          phew!!!, it seems to be workin now, i put the datafile.close inside the try block. how would i display it in a table though, the code is rather large for a table.

          Code:
          <%@ page import="java.util.*,java.io.*" %>
          <%@ page import="testBean.TestBean" %>
          
          
          <html>
          <head>
          
          <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
          
          </head>
          
          <body>
          
          <hr></hr>
          
          <center>
          
          <h1> 
          
           Weather Forecast</h1>
           
           <%
           
            String valString;
             try { 
           
             int recCount = 0;
          	FileReader dFile = new FileReader("w:/www/DataFile.txt");
          	   BufferedReader dataFile = new BufferedReader(dFile);
          	valString = dataFile.readLine();
          	while(valString != null){
          	 recCount++;
          	 out.println(valString);
          	 valString = dataFile.readLine();
          	 dataFile.close();
          	}
             }
             catch (FileNotFoundException e) {
             System.err.println("FileStreamsTest: " + e);
            }
            catch(IOException iOE) {
             iOE.printStackTrace();
            }
            finally {
             //dataFile.close();  
            }
             
           
          %>
           
           <jsp:useBean id="temp"  class="TestBean" /> 
           
           <br><br><br>
          
          <a href="test.html"> Local Five day forecast </a>
          
          <br><br><br><br><br><br><br>
          <table>
          <tr><TD><IMG SRC="sunny.gif" ALT="S" BORDER=0 WIDTH=100 HEIGHT=100 ></TD><TD><h4> <%= temp.post() %> </h4></TD> </tr>
          </table>
          
          <br><br><br><br><br><br><br>
          
          <hr></hr>
          
          </body>
          </html>

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #20
            Originally posted by Naha
            phew!!!, it seems to be workin now, i put the datafile.close inside the try block. how would i display it in a table though, the code is rather large for a table.

            Code:
             
            <%@ page import="java.util.*,java.io.*" %>
            <%@ page import="testBean.TestBean" %>
             
             
            <html>
            <head>
             
            <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
             
            </head>
             
            <body>
             
            <hr></hr>
             
            <center>
             
            <h1> 
             
            Weather Forecast</h1>
             
            <%
             
            String valString;
            try { 
             
            int recCount = 0;
            	FileReader dFile = new FileReader("w:/www/DataFile.txt");
            	 BufferedReader dataFile = new BufferedReader(dFile);
            	valString = dataFile.readLine();
            	while(valString != null){
            	 recCount++;
            	 out.println(valString);
            	 valString = dataFile.readLine();
            	 dataFile.close();
            	}
            }
            catch (FileNotFoundException e) {
            System.err.println("FileStreamsTest: " + e);
            }
            catch(IOException iOE) {
            iOE.printStackTrace();
            }
            finally {
            //dataFile.close(); 
            }
             
             
            %>
             
            <jsp:useBean id="temp" class="TestBean" /> 
             
            <br><br><br>
             
            <a href="test.html"> Local Five day forecast </a>
             
            <br><br><br><br><br><br><br>
            <table>
            <tr><TD><IMG SRC="sunny.gif" ALT="S" BORDER=0 WIDTH=100 HEIGHT=100 ></TD><TD><h4> <%= temp.post() %> </h4></TD> </tr>
            </table>
             
            <br><br><br><br><br><br><br>
             
            <hr></hr>
             
            </body>
            </html>
            I suggest you put the close statement back in the finally so it always gets executed. To display the contents of the file in a table you just mix the html with the java code. You haven't said what you want to form the heading of the table and the rows and columns yet.

            Comment

            • Naha
              New Member
              • Feb 2007
              • 41

              #21
              Something like this: 3 rows and 2 columns.

              Code:
                            Todays’ Forecast
              Temperature	(first line of data)
              Light	                (second line of data)
              Thank you so much for all your help, I am really greatefull.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #22
                Originally posted by Naha
                Something like this: 3 rows and 2 columns.

                Code:
                Todays’ Forecast
                Temperature	(first line of data)
                Light	 (second line of data)
                Thank you so much for all your help, I am really greatefull.
                You'd have to have Temperature and light in an array

                Something like

                Code:
                 <table>
                 <tr>
                  <th></th>
                  <th></th> 
                 </tr>
                <%
                 
                  String valString;
                  BufferedReader dataFile = null;
                   try {
                   String[] temp = {"Temperature", "Ligth"};
                   int recCount = 0;
                	FileReader dFile = new FileReader("w:/www/DataFile.txt");
                	   dataFile = new BufferedReader(dFile);
                	valString = dataFile.readLine();
                	while(valString != null){
                  %>
                  <tr>
                  <td><%=temp[recCount]%></td>
                  <td><%=valString%></td>
                 </tr>
                <%
                	 recCount++;
                	 //out.println(valString);
                	 valString = dataFile.readLine();
                	}
                   }
                   catch (FileNotFoundException e) {
                   System.err.println("FileStreamsTest: " + e);
                  }
                  catch(IOException iOE) {
                   iOE.printStackTrace();
                  }
                  finally {
                   dataFile.close();
                  }
                
                %>
                
                </table>
                Might have to modify it a bit. I have to go. Good luck with the rest of it

                Comment

                • Naha
                  New Member
                  • Feb 2007
                  • 41

                  #23
                  ok, thanks for your help.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #24
                    Originally posted by Naha
                    ok, thanks for your help.
                    Did you get it to work like you wanted?

                    Comment

                    Working...