Results 1 to 15 of 15

Thread: regex problem

  1. #1
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default 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;
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: regex problem

    Quote Originally Posted by fishy8158 View Post
    I run the program and nothing happens
    Crystal ball says you are swallowing an exception.

  4. #4
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default 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.

  5. #5
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default 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;
    
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: regex problem

    What is the actual error message? Always show these if you are asking a question regarding a compiler error.

  7. #7
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default 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();
    	}
    }
    }

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: regex problem

    private StringBuffer sb;

  9. #9
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: regex problem

    it shouldn't be there? line 65
    its used in both methods.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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.

  12. #12
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default 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.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: regex problem

    Quote Originally Posted by fishy8158 View Post
    I fixed subprogram so it only has 1 method, so i dont need the instance fields anymore.
    Where in any of the replies so far has anyone suggested that you cram all your code into one method?

  15. #15
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: regex problem

    Quote Originally Posted by Junky View Post
    Where in any of the replies so far has anyone suggested that you cram all your code into one method?
    yea i realize it wasn't a good idea. however i got everything to work with one method. ima seperate it again.

Similar Threads

  1. regex problem
    By javaPower in forum Advanced Java
    Replies: 7
    Last Post: 10-30-2011, 07:15 AM
  2. How to use regex and split to solve a problem
    By PeteClimbs in forum New To Java
    Replies: 2
    Last Post: 04-19-2011, 08:31 PM
  3. breaking up a string, a regex problem!!
    By A.n.H in forum Advanced Java
    Replies: 7
    Last Post: 05-18-2010, 02:39 AM
  4. breaking up a string, a regex problem!!
    By A.n.H in forum Advanced Java
    Replies: 0
    Last Post: 05-17-2010, 03:03 PM
  5. Regex problem
    By Nimyz in forum Advanced Java
    Replies: 4
    Last Post: 05-14-2010, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •