• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Search Problem

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to find http links in a web page.I am writing a code for it.I'm searching line by line but if the web page code, has more than one http link, my code finds only one link.What should I do?
This is my code;

import java.net.*;
import java.lang.*;
import java.util.regex.*;
import java.util.*;

public class Linkler2{

public static void main(String args[]) throws IOException
{
String sayi;
int abc;

URL local = new URL("http://www.google.com.tr");
URLConnection baglanti = local.openConnection();

BufferedReader oku = new BufferedReader(new InputStreamReader(baglanti.getInputStream()));
PrintWriter dataOut = new PrintWriter(new FileWriter("abc2.txt"),true);

try
{

int deger=0;

int kelime=0;

while ((sayi = oku.readLine()) != null)
{
System.out.println(sayi);
kelime= sayi.indexOf("http://");
System.out.println(kelime);
if (kelime!=-1)
{
deger=deger+1;
}

dataOut.println(sayi);

}

System.out.println(deger);
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're using String's indexOf(str) method, which finds the first occurrence of the str argument. You will probably want to use String's indexOf(str, int) method, which begins searching at the indicated index.

So, for example, if you find your first occurrence at index 108, then you will want to start searching for your next occurrence at index 109.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably you might be getting whole page soure in one line and the method indexOf("http://") finds the first occurence and gets out.

cheers,
shankar.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use HTMLUnit. It's amazing how OOPy your code can get with HTMLUnit, instead of all this raw String manipulation.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic