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;
}
}
}
Non-repeating information grab
Page 1 of 14 Replies - 151 Views - Last Post: 25 December 2012 - 10:06 PM
#1
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?
Replies To: Non-repeating information grab
#2
Re: Non-repeating information grab
Posted 25 December 2012 - 06:16 PM
Just a little tip for you...
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.
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
#3
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;
}
}
}
#4
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.
#5
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
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
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|