hi I have the following quotes separated by % here : http://fortunes.cat-v.org/
I'm trying to separate the quotes already stored in a List (cookies) via this method and display them one by one, at random, after each page refresh:
public class CookieJar {
static List<String> cookies = new ArrayList<>();
static int index = 0;
/* read the page, store into rawCookiesString */
public static void getUrlContent () throws SecurityException{
try {
URL url = new URL("http://fortunes.cat-v.org/openbsd/");
BufferedReader in = new BufferedReader (new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
cookies.add(line);
}
in.close();
}
catch (java.net.MalformedURLException e) {
System.out.println("Malformed url" + e.getMessage());
}
catch (IOException e) {
System.out.println("I/O Error" + e.getMessage());
}
}
public static String proccessString () {
String result =" ";
if (index == 0) {
index++;
getUrlContent();
}
else if (!cookies.isEmpty() && index >= 0) {
for (String cookie:cookies) {
Pattern pattern = Pattern.compile("%([^%]+)%");
Matcher m = pattern.matcher(cookie);
while (m.find()) {
result = m.group(1).toString();
Collections.shuffle(cookies);
}
}
} else {
System.out.println("Better luck next time");
}
return result;
}
}
the first method stores the quotes in the cookies List like so:
, %, (1) Alexander the Great was a great general., (2) Great generals are forewarned., (3) Forewarned is forearmed., (4) Four is an even number., (5) Four is certainly an odd number of arms for a man to have., (6) The only number that is both even and odd is infinity., , Therefore, Alexander the Great had an infinite number of arms., %, (1) Everything depends., (2) Nothing is always., (3) Everything is sometimes., %, 1.79 x 10^12 furlongs per fortnight -- it's not just a good idea, it's, the law!, %, 10.0 times 0.1 is hardly ever 1.0., %,
I want the second method to extract a single quote from in between the % at random and display it like so:
1.79 x 10^12 furlongs per fortnight -- it's not just a good idea, it's, the law!
currently all I get are random non nonsensical portions of the quotes every time I refresh