Results 1 to 15 of 15
Thread: regex problem
- 11-24-2011, 01:25 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
regex problem
Im making a program that finds the links in a site,and prints it. Compiler doesn't tell me i have any errors. I run the program and nothing happens, can anyone tell me what i am doing wrong? this is part of it.
Java Code:public String href(StringBuffer asb) { link=Pattern.compile("href=\"[^>]*\">"); sb=asb; Matcher linkmatches=link.matcher(sb.toString()); while(linkmatches.find()){ String s=linkmatches.group().replace("href=\\" , " ").replace("\">", ""); if(s.matches("http://.*")){ links.add(s); } else { badlinks.add(s); } } for(int i=0;i<links.size();i++) System.out.println(links.get(i)); return "end of links"; } private Pattern link; private String str; private StringBuffer sb; }
-
Re: regex problem
Myself, I'd use Jsoup for easy and clean HTML parsing.
- 11-24-2011, 01:30 AM #3
- 11-24-2011, 01:46 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: regex problem
@fub, its for an hw assignment, professor wants us to use pure code. but thanks for advice
@junky i added throws PatternSyntaxException . seems like its still not the case. but thanks.
if theres no problem with this, then should be the "reading url" class.
- 11-24-2011, 02:03 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: regex problem
this is the sub program, now compiler complains about line 44, but i don't understand whats wrong.
44: try to match "href=\"[^>]*\">" in the StringBuffer which has the website's content.
Thanks for any help in advace
Java Code:import java.io.BufferedReader; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.regex.*; import java.util.ArrayList; public class LinkFinder { ArrayList<String> links=new ArrayList(); ArrayList<String> badlinks=new ArrayList(); public LinkFinder(){ } public void findLink(URL u) throws MalformedURLException,IOException{ try{ HttpURLConnection uc = (HttpURLConnection) u.openConnection(); InputStream in = new BufferedInputStream(uc.getInputStream()); Reader r = new InputStreamReader(in); BufferedReader br=new BufferedReader(r); StringBuffer sb=new StringBuffer(); br.toString(); while((str=br.readLine())!=null){ sb.append(str); System.out.println(str); } this.href(); in.close(); r.close(); br.close(); }catch(MalformedURLException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } } public String href() throws PatternSyntaxException { try{ link=Pattern.compile("href=\"[^>]*\">"); Matcher linkmatches=link.matcher(sb.toString()); while(linkmatches.find()){ String s=linkmatches.group().replace("href=\\" , " ").replace("\">", ""); if(s.matches("http://.*")){ links.add(s); } else { badlinks.add(s); } } for(int i=0;i<links.size();i++) System.out.println(links.get(i)); } catch(PatternSyntaxException e){ e.printStackTrace(); System.out.println("pattern errorrrrrr"); } return "end of links"; } private Pattern link; private String str; private StringBuffer sb; }
-
Re: regex problem
What is the actual error message? Always show these if you are asking a question regarding a compiler error.
- 11-24-2011, 02:37 AM #7
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: regex problem
compiler errorr:
java.lang.NullPointerException
at LinkFinder.findLink(LinkFinder.java:44)
at LinkFinderTester.main(LinkFinderTester.java:17)
main class:
Java Code:import java.net.URL; import java.net.MalformedURLException; import java.io.*; public class LinkFinderTester { public static void main(String[] args) throws IOException,MalformedURLException{ try{ String address="http://java.sun.com/index.html"; URL u =new URL(address); LinkFinder lf=new LinkFinder(); lf.findLink(u);} catch(MalformedURLException e){ e.printStackTrace(); } } }
- 11-24-2011, 02:43 AM #8
Re: regex problem
private StringBuffer sb;
- 11-24-2011, 02:54 AM #9
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: regex problem
it shouldn't be there? line 65
its used in both methods.
-
Re: regex problem
Your code is very difficult to read and impossible to debug with your crappy code indentation. You've likely got a scope issue where your StringBuffer variable has been declared and constructed in one method but the class's StringBuffer field is still null, but to be honest, I have no idea where one method ends and one begins because your formatting is gawd-awful. You must correct it at once. If you want volunteers to put in the effort to understand your code, you should at least put in the effort so that it's not difficult for them to do.
- 11-24-2011, 04:23 AM #11
Re: regex problem
Yeah I was trying to highlight the fact that your class level sb variable is null. In the findLink method you create a local sb variable. In the href method you use the sb variable but it only has access to the class level one which is still null => NullPointerException.
- 11-24-2011, 04:27 AM #12
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: regex problem
Sorry about that. ill try to learn that now, my professor never really talked about it.
I fixed subprogram so it only has 1 method, so i dont need the instance fields anymore.
the program can run but all the links it print are "badlinks".
And thanks for pointing that out. I didn't notice i created a local sb.
-
Re: regex problem
Look at the code examples from the "pros" in this and other forums to see how to properly indent your code. Also your book and the tutorials will show you how.
- 11-24-2011, 05:03 AM #14
- 11-24-2011, 03:59 PM #15
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
regex problem
By javaPower in forum Advanced JavaReplies: 7Last Post: 10-30-2011, 07:15 AM -
How to use regex and split to solve a problem
By PeteClimbs in forum New To JavaReplies: 2Last Post: 04-19-2011, 08:31 PM -
breaking up a string, a regex problem!!
By A.n.H in forum Advanced JavaReplies: 7Last Post: 05-18-2010, 02:39 AM -
breaking up a string, a regex problem!!
By A.n.H in forum Advanced JavaReplies: 0Last Post: 05-17-2010, 03:03 PM -
Regex problem
By Nimyz in forum Advanced JavaReplies: 4Last Post: 05-14-2010, 07:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks