SRL Archive - Java Help and Tutorials

How to use Between in Java?


rj

2013-10-09 21:49:34

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

2013-10-10 17:36:01

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

2013-10-12 13:50:03

From: Kevin
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

2013-10-12 15:38:53

From: Officer Barbrady
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

2013-10-12 16:07:35

From: Kevin
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

2013-10-12 18:17:39

From: Officer Barbrady
-.- how can i make it just do between without putting in all that >??>>>!>!># crap?


Write your own parser.

rj

2013-10-12 18:18:48

From: Kevin
Write your own parser.


can you write one for me :)

Syntax

2013-10-27 06:38:25

From: Officer Barbrady
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

2013-10-27 08:10:31

From: Syntax
@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

2013-10-27 08:46:03

Yeah you can never go wrong with Stack Overflow :D

Kasi

2013-10-27 12:08:26

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

2013-10-27 12:38:11

From: Kasi
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

2013-10-27 13:30:41

From: Brandon
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 ;)