0

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

  • can you please show us input output example? – Youcef LAIDANI May 01 '17 at 12:59
  • the bullet points are the output. random snipes from the quotes. the cookies List is the input –  May 01 '17 at 13:15
  • Input vs output. We got what you see as output. Beyond that: your method is doing way too many things. Like: why using a compiled pattern ... when you keep compiling over and over again in your method? You should really slice your code into multiple methods, each one doing **one** thing, not 5. Would also make it **much** easier for you to test your code. – GhostCat May 01 '17 at 13:20
  • if we forget about the method and only focus on regex. is the expression working as it should meaning gathering the text between % ? @GhostCat –  May 01 '17 at 13:33
  • What if you use `Pattern.compile("%([^%]+)%")`? – Wiktor Stribiżew May 01 '17 at 13:44
  • @WiktorStribiżew same thing –  May 01 '17 at 13:49
  • I updated the code and added the full code. –  May 01 '17 at 14:09
  • Instead of the code you have, you'd better post the string you get, and the expected output you need to extract from it. – Wiktor Stribiżew May 01 '17 at 14:38
  • @WiktorStribiżew I updated the question if it helps. thanks –  May 01 '17 at 15:16
  • [*Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.* - Jamie Zawinski](http://regex.info/blog/2006-09-15/247) –  May 01 '17 at 15:17

0 Answers0