Results 1 to 13 of 13

Thread: How to use Between in Java?

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default How to use Between in Java?

    Having trouble using 'between' in java...

    I get this error:

    Code:
    Exception in thread "main" java.lang.IllegalStateException: No match found
    	at java.util.regex.Matcher.group(Unknown Source)
    	at Highscores.main(Highscores.java:46)
    with this code:

    java Code:
    Pattern p = Pattern.compile("\\" + SkillsTSA[i] + "</a></td><td>.*\\</td><td>");
                Matcher m = p.matcher(Source);
                m.find();
                System.out.println(SkillsTSA[i] + " " + m.group(1));

    Whole source:
    java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;


    public class Highscores {
       
         static String Source;
         public static String getUrlSource(String url) throws IOException {
             URL page = new URL(url);
             URLConnection yc = page.openConnection();
             BufferedReader in = new BufferedReader(new InputStreamReader(
                     yc.getInputStream(), "UTF-8"));
             String inputLine;
             StringBuilder a = new StringBuilder();
             while ((inputLine = in.readLine()) != null)
                 a.append(inputLine);
             in.close();

             return a.toString();
         }
         
         public static void main(String args[]) {
             String [] SkillsTSA = {
                 "Attack", "Defence", "Strength", "Hitpoints", "Ranged",
                 "Prayer", "Magic", "Cooking", "Woodcutting", "Fletching",
                 "Fishing", "Firemaking", "Crafting", "Smithing", "Mining",
                 "Herblore", "Agility", "Thieving", "Slayer", "Farming",
                 "Runecrafting", "Dungeoneering", "Summoning"
              };
            // String name = "";
             try {
                Source = getUrlSource("http://soulsplit.com/hs/index.php?name=advise");
            } catch (IOException e) {
                System.out.println("Error getting page source");
            }

            for (int i = 0; i < SkillsTSA.length; i++) {
                Pattern p = Pattern.compile("\\" + SkillsTSA[i] + "</a></td><td>.*\\</td><td>");
                Matcher m = p.matcher(Source);
                m.find();
                System.out.println(SkillsTSA[i] + " " + m.group(1));
            }
         }

    }



    why can't it find the inbetween string..

  2. #2
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    Try this in place of where you currently do the find:
    Code:
    if(m.find()){
                System.out.println(SkillsTSA[i] + " " + m.group(1));
     } else {
                System.out.println("I am a noob who does not know how to create valid regexes. -Rjj") 
    }

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Kevin View Post
    Try this in place of where you currently do the find:
    Code:
    if(m.find()){
                System.out.println(SkillsTSA[i] + " " + m.group(1));
     } else {
                System.out.println("I am a noob who does not know how to create valid regexes. -Rjj") 
    }
    Code:
    I am a noob who does not know how to create valid regexes. -Rjj
    Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1
    	at java.util.regex.Matcher.group(Unknown Source)
    	at Grabber.Highscores.main(Highscores.java:47)
    Still didn't work lol

  4. #4
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    Code:
    I am a noob who does not know how to create valid regexes. -Rjj
    Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1
    	at java.util.regex.Matcher.group(Unknown Source)
    	at Grabber.Highscores.main(Highscores.java:47)
    Still didn't work lol
    Actually, it did work

    It's telling you that your regex won't find anything and either you're grabbing an invalid string, or that regex doesn't exist in the string you're searching through. So, the code you think is the problem, isn't. At least not directly.

  5. #5
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Kevin View Post
    Actually, it did work

    It's telling you that your regex won't find anything and either you're grabbing an invalid string, or that regex doesn't exist in the string you're searching through. So, the code you think is the problem, isn't. At least not directly.
    -.- how can i make it just do between without putting in all that >??>>>!>!># crap?

  6. #6
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    -.- how can i make it just do between without putting in all that >??>>>!>!># crap?
    Write your own parser.

  7. #7
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Kevin View Post
    Write your own parser.
    can you write one for me

  8. #8
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    418
    Mentioned
    2 Post(s)
    Quoted
    86 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    can you write one for me
    @Officer Barbrady
    I suggest going to Stack Overflow. Very good website for answer on coding quick too and free.

  9. #9
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Syntax View Post
    @Officer Barbrady
    I suggest going to Stack Overflow. Very good website for answer on coding quick too and free.
    Stack overflow is were I got the answer :S

  10. #10
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    418
    Mentioned
    2 Post(s)
    Quoted
    86 Post(s)

    Default

    Yeah you can never go wrong with Stack Overflow

  11. #11
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    Seriously? between?? look at how srl/simba uses it.

    Code:
    	private static int Pos(String Needle, String Haystack) {
    		for (int I = 0; I <= Haystack.length() - Needle.length(); I++) {
    			if (Haystack.substring(I, I + Needle.length()).compareTo(Needle) == 0)
    				return I;
    		}
    		return -1;
    	}
    
    	private static String Between(String strTemp, String Begin, String End) {
    		int S = Pos(Begin, strTemp);
    		if (S == -1)
    			return "";
    		return strTemp.substring(S, Pos(End, strTemp) + End.length());
    	}

  12. #12
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by Kasi View Post
    Seriously? between?? look at how srl/simba uses it.

    Code:
    	private static int Pos(String Needle, String Haystack) {
    		for (int I = 0; I <= Haystack.length() - Needle.length(); I++) {
    			if (Haystack.substring(I, I + Needle.length()).compareTo(Needle) == 0)
    				return I;
    		}
    		return -1;
    	}
    
    	private static String Between(String strTemp, String Begin, String End) {
    		int S = Pos(Begin, strTemp);
    		if (S == -1)
    			return "";
    		return strTemp.substring(S, Pos(End, strTemp) + End.length());
    	}
    Damn.. That pos func :l

    ={ I thought we had the understanding that Simba contains over-complicated code when translating to another language :l

    I'd personally do it like:

    Java Code:
    public static int pos(String Needle, String Haystack) {
        return Haystack.indexOf(Needle);
    }

    public static int posEx(String Needle, String Haystack, int Start) {
        return Haystack.indexOf(Needle, Start);
    }

    public static String between(String Str, String Start, String End) {
        int i = Str.indexOf(Start);
        if (i != -1) {
            int j = Str.indexOf(End, i + Start.length());
            return j != -1 ? Str.substring(i + Start.length(), j) : null;
        }
        return null;
    }

    public static String betweenEx(String Str, String Start, String End) {
        final Pattern m = Pattern.compile(Start + "(.*)" + End);
        Matcher matcher = m.matcher(Str);
        return matcher.find() ? matcher.group(1) : null;
    }

    public static void main(String[] args) {
        System.out.println(between("<td>Hello</td>", "<td>", "</td>"));
        System.out.println(betweenEx("<td>Hello</td>", "<td>", "</td>"));
    }
    I am Ggzz..
    Hackintosher

  13. #13
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    Damn.. That pos func :l
    Why do you always undermine me!

    I was clearly teaching him the understanding that he could translate simba code directly into Java code

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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