0

I have a concept but I'm not sure how to go at it. I would like to parse a website and use regex to find certain parts. Then store these parts into a string. After I would like to do the same, but find differences between before and after.

The plan:

  1. parse/regex add lines found to the array before.
  2. refresh the website/parse/regex add lines found to the array after.
  3. compare all strings before with all of string after. println any new ones.
  4. send all after strings to before strings.

Then repeat from 2. forever.

Basically its just checking a website for updated code and telling me what's updated.

Firstly, is this doable?

Here's my code for part 1.

String before[] = {};
int i = 0;
while ((line = br.readLine()) != null) {
    Matcher m = p.matcher(line);
    if (m.find()) {
        before[i]=line;
        System.out.println(before[i]);  
        i++;    
    }
}

It doesn't work and I am not sure why.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • `String before[] = {};` is a zero-length array. Also arrays are static in size, once initialized they can't grow. But you can use a list instead, `List befores = new ArrayList();`. Also please check this page - http://stackoverflow.com/a/1732454/738746, here on SO, the summary is that you should use some HTML parser for that job. – Bhesh Gurung Mar 26 '14 at 23:53

1 Answers1

0

You could do something like this, assuming you're reading from a file:

Scanner s = new Scanner(new File("oldLinesFilePath"));
List<String> oldLines = new ArrayList<String>();
List<String> newLines = new ArrayList<String>();

while (s.hasNext()){
    oldLines.add(s.nextLine());
}

s = new Scanner(new File("newLinesFilePath"));

while (s.hasNext()){
    newLines.add(s.nextLine());
}    
s.close();

for(int i = 0; i < newLines.size(); i++) {
   if(!oldLines.contains(newLines.get(i)) {
       System.out.println(newLines.get(i));
   }
}
Josh Roberts
  • 862
  • 6
  • 12