4 Replies - 323 Views - Last Post: 25 December 2012 - 10:06 PM Rate Topic: -----

#1 CorporalTurnips  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-December 12

Non-repeating information grab

Posted 25 December 2012 - 05:46 PM

I am trying to make a program that will grab information line by line from a .txt document on the web and one that will continue to do so as the information is updated. So far I have this code and it does grab it from the website line by line, but when I upload an updated .txt file with more lines, the program doesn't grab the new ones. How can I make it do that?

try
        {
            URL url = new URL("http://www.zachburtle.com/Ye.txt");
            URLConnection connection = url.openConnection();
            connection.setDoInput(true);
            InputStream inStream = connection.getInputStream();
            BufferedReader input = new BufferedReader(new InputStreamReader(inStream));
            
            String line = ".";
			int previousCount = 0;
            
            while(true)
            {
				int count = 1;
				line = input.readLine();
			
                while(line != null)
                {
					if (line != null)
					{
						previousCount = count;
						++count;
					}
					
					if(previousCount < count && line != null)
					{
						System.out.println(line);
					}
					
					line = null;
                }
            }
            
            
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Non-repeating information grab

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Non-repeating information grab

Posted 25 December 2012 - 06:16 PM

Just a little tip for you...

while(line != null)
{
     if (line != null) //<-- Not needed, we already know line is not null because of line above.
     {
          previousCount = count;
          ++count;
     }

     if(previousCount < count && line != null) // <-- Again, no need to test "line" again against null




No use testing to see if line is not null again, the while condition makes sure that it isn't. You are just repeating the test.

Now another thing, you set previousCount equal to count. Then increment it. What for? You then test if it is less than count. AFTER the loop set the previousCount. In the while loop check count against previousCount. You don't want to be resetting previousCount each time through the loop.

So fix those up and let's see what you have. :)

This post has been edited by Martyr2: 25 December 2012 - 06:20 PM

Was This Post Helpful? 2
  • +
  • -

#3 CorporalTurnips  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-December 12

Re: Non-repeating information grab

Posted 25 December 2012 - 06:22 PM

Thank you! I didn't even notice that. I now have this:

try
        {
            String line = ".";
			int previousCount = 0;
            
            while(true)
            {
				URL url = new URL("http://www.zachburtle.com/Ye.txt");
				URLConnection connection = url.openConnection();
				connection.setDoInput(true);
				InputStream inStream = connection.getInputStream();
				BufferedReader input = new BufferedReader(new InputStreamReader(inStream));

				int count = 1;
				line = input.readLine();
			
                while(line != null)
                {
					previousCount = count;
					++count;
					System.out.println(line);
					line = null;
                }

            }
            
            
        }


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: Non-repeating information grab

Posted 25 December 2012 - 06:33 PM

It really doesn't make sense to constantly check for updates. I believe you can use Java Web Start to run your program every hour or so, though I'm not that familiar with that technology.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: Non-repeating information grab

Posted 25 December 2012 - 10:06 PM

You miss the whole concept here. That makes us scary about what you do further

               while(line != null)                  {  
                    if (line != null)  


If the while() check that the line is not null, why do you check in a if() right after that line is not null ? Sure that it can't be :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1