storing data in an array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Naha
    New Member
    • Feb 2007
    • 41

    #1

    storing data in an array.

    Hi Guys,

    this is the code that reads the data from a text file and displays it in a jsp.
    iam trying to put the data in a array so that each time the code readfs the data the previous data is stored, however this is not working can someone please help.


    Code:
    <HTML>
    <HEAD>
    <TITLE>The result.....</TITLE>
    </HEAD>
    <BODY>
     
    <%@ page language = "java" import="java.io.*"%>
     
    <% try
    	{
    		
    			
    	String strPath ="W:/www/ee05u041/data.txt";
    	String j;
    	int []i;
    	i=new int[10];
    
    	for( i[0]; i[0]<10; i++)
    {
       BufferedReader objReader = new BufferedReader(new FileReader(strPath));
    		i[0] = objReader.readLine ();
     		j=Integer.parseInt(i[0]);
    		
    	while (i[0] != null)
    	
    	{
    		out.println(j);
    		j = objReader.readLine();
    		i[0]=j;
    	}
    	
    	}
    	out.println(i[0]);
    		out.println(i[1]);
    			out.println(i[2]);
     
    	catch(IOException e){} 
    	}
    %>	
     
    </BODY>
    </HTML>
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    (There is actually a jsp forum but....)

    The way you are using the array is a little strange.....
    Your forloop increments the VALUE of i[0]....

    try changing the for loop to something like
    Code:
    for( int k=0; k<10; k++)
    {
       BufferedReader objReader = new BufferedReader(new FileReader(strPath));
       i[k] = objReader.readLine ();
       j=Integer.parseInt(i[k]);
    		
       while (i[k] != null)	
      {
        out.println(j);
        j = objReader.readLine();
        i[k]=j;
      }
    	
    }
    out.println(i[0]);
    out.println(i[1]);
    out.println(i[2]);

    Comment

    Working...