File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Search Problem

 
Ekrem Altintas
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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);
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Shankar Narayana
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Stuart Ash
Ranch Hand
Posts: 637
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic