Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading certain HTML lines

Status
Not open for further replies.

RENO9

Technical User
Dec 8, 2005
27
GB
Im using the code below to start reading a URL at line 300 but i would like it to stop reading at line 360, as im a java novice could you indicate where im going wrong, every seems ok (i.e. starts at 300) but it does not stop at the required point?

Code:
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
 
public class NetFile
{
  public static void main(String args[])
  {
    String data;
    DataInputStream file = null;
    int post = 300;		//Starting line
    String url = "[URL unfurl="true"]http://www.javaworld.com/columns/jw-tips-index.shtml";[/URL] //Example
    
    try
    {
      file = new DataInputStream((new URL(url)).openStream());
      data = file.readLine();
      
      while (data != null)
      {
        for(int x = 0; x < 360;)
        {
           while(x >= post)
            {
              System.out.println(data);
              data=file.readLine();
              StringTokenizer st = new StringTokenizer(data);
              while(st.hasMoreElements())
              {
                String s = st.nextToken();
                System.out.print(x + "\t" + s);
                for(double j = 0; j < 1000000; j++) {}  //Readable output
              }
              x++;
            }
            data=file.readLine();
          x++;  
        }
      }
      
    }
    catch(IOException e)
    {
      System.out.println("Error : " + e);
    }
  }
}

Any suggestions where im going wrong?
Thanks
 
Can u still use DataInputStream? I thought that was deprecated. And where are you closing the stream at?

~mp
 
After looking at it further, your problem has to do with the while loop

Code:
while(x >= post) {. . . }

You are going in an infinite loop that loops while x is greater than 300. you could get rid of whole for loop, and just have

Code:
while (data != null) {
    int x = post;
    while(x >= post && x < 360) {. . .}
}

this will loop between 300 and 360

~mp
 
@thumpkidd9:
It will not, since he likes to skip the first 300 lines.
How's about that:
Code:
while (data != null)
{
	for (int x = 0; x < 360; ++x)
	{
		data=file.readLine ();
		if (x >= post)
		{
			System.out.println (data);
			StringTokenizer st = new StringTokenizer (data);
			while (st.hasMoreElements ())
			{
				String s = st.nextToken ();
				System.out.print (x + "\t" + s);
				for (double j = 0; j < 1000000; j++) {}  //Readable output
			}
		}
	}
}

seeking a job as java-programmer in Berlin:
 
Thanks for the help but its not sorted with
Code:
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
 
public class NetFile
{
  public static void main(String args[])
  {
    String data;
    DataInputStream file = null;
    int post = 300;		//Starting line
    String url = "[URL unfurl="true"]http://www.javaworld.com/columns/jw-tips-index.shtml";[/URL] //Example
    
    try
    {
      file = new DataInputStream((new URL(url)).openStream());
      data = file.readLine();
      
      int x = 0;
      int limit = 360; 
      while (data != null)
      {
           if(x >= post && x <= limit)
            {
              System.out.println(data);
              data=file.readLine();
              StringTokenizer st = new StringTokenizer(data);
              while(st.hasMoreElements())
              {
                String s = st.nextToken();
                System.out.print(x + "\t" + s);
                for(double j = 0; j < 1000000; j++) {}  //Readable output
              }
            }
            data=file.readLine();
            x++;
        }
      }
    catch(IOException e)
    {
      System.out.println("Error : " + e);
    }
  }
}
 
Sorry a typo, "Thanks for the help but its 'got' sorted with"...

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top