Loading...
Logo
Processing Forum
Hi, I'm trying to get the meteorologic status of the day into processing analysing the html code on a website.

The thing is, I got the html code into an array of strings type into processing, that was easy, did some search and got it together, but I can't figure out how to compare a string with one of the lines of the array.

I tried this:

Copy code
  1. String lines[] = loadStrings("http://www.meteo.pt/pt/html_prev.jsp");

  2. String detect = "<td rowspan=\"2\">Lisboa</td>";
  3. char cline, cdetect;

  4. for (int i=0; i < lines.length; i++) {
  5.   for (int n=0; n < 24 /*lines[i].charAt(n) != "\n"*/; n++)
  6.   {
  7.     cline = lines[i].charAt(n);
  8.     cdetect = detect.charAt(n);
  9.     
  10.     if (lines[i] == null)
  11.       break;
  12.     
  13.     println(n);
  14.     if (cline != cdetect)
  15.       break;
  16.     else if (cline == cdetect && n == 23)
  17.       println("Event detected on line " +i);
  18.   }
  19. }
But it gave me a "StringIndexOutOfBoundsException: String index out of range:0" on line 9.

Then I tried this:

Copy code
  1. String lines[] = loadStrings("http://www.meteo.pt/pt/html_prev.jsp");

  2. String detect = "<td rowspan=\"2\">Lisboa</td>";
  3. char cline, cdetect;

  4. for (int i=0; i < lines.length; i++) {
  5.   for (int n=0; n < 24 /*lines[i].charAt(n) != "\n"*/; n++)
  6.   {
  7.     try {
  8.       cline = lines[i].charAt(n);
  9.       cdetect = detect.charAt(n);
  10.     } 
  11.     catch (IOException e) {
  12.       lines[i] = null;
  13.     }
  14.     if (lines[i] == null)
  15.       break;

  16.     try {
  17.       cline = lines[i].charAt(n);
  18.       cdetect = detect.charAt(n);
  19.     } 
  20.     catch (IOException e) {
  21.     }
  22.     println(n);
  23.     if (cline != cdetect)
  24.       break;
  25.     else if (cline == cdetect && n == 23)
  26.       println("Event detected on line " +i);
  27.   }
  28. }
and it gave me "Unreachable block for IOException. This exception is never thrown from the try statement body" on line 13, but the syntax for the try statement is like this in the reference, what am I doing wrong?

Thanks

Replies(2)

Note that there is a tab in that line. If you run this code...
Copy code
  1. void setup() {
  2.   String lines[] = loadStrings("http://www.meteo.pt/pt/html_prev.jsp");
  3.   println("There are " + lines.length + " lines.");
  4.   String detect = "    <td rowspan=\"2\">Lisboa</td>";
  5.   for (int i=0; i<lines.length; i++) {
  6.     if (lines[i].equals(detect)) {
  7.       println("String detected on line: " + i);
  8.     }
  9.   }
  10.   exit();
  11. }
...it will print the following to the console...
Copy code
  1. There are 3525 lines.
  2. String detected on line: 603
  3. String detected on line: 1033
  4. String detected on line: 1961
  5. String detected on line: 2429
wow! That was the problem! You know, I tried the equals() function before, but I couldn't get anything so I tried to compare char by char to see what happens. There was a tab, and that answers everything! Thanks amnon.owed!