PDA

View Full Version : How to use Between in Java?



rj
10-10-2013, 01:49 AM
Having trouble using 'between' in java...

I get this error:


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:

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

Kevin
10-10-2013, 09:36 PM
Try this in place of where you currently do the find:


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")
}

rj
10-12-2013, 05:50 PM
Try this in place of where you currently do the find:


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")
}


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

Kevin
10-12-2013, 07:38 PM
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 :p

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.

rj
10-12-2013, 08:07 PM
Actually, it did work :p

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?

Kevin
10-12-2013, 10:17 PM
-.- how can i make it just do between without putting in all that >??>>>!>!># crap?

Write your own parser.

rj
10-12-2013, 10:18 PM
Write your own parser.

can you write one for me :)

Syntax
10-27-2013, 10:38 AM
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.

rj
10-27-2013, 12:10 PM
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

Syntax
10-27-2013, 12:46 PM
Yeah you can never go wrong with Stack Overflow :D

Kasi
10-27-2013, 04:08 PM
Seriously? between?? look at how srl/simba uses it.



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

Brandon
10-27-2013, 04:38 PM
Seriously? between?? look at how srl/simba uses it.



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:


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>"));
}

Kasi
10-27-2013, 05:30 PM
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 ;)