1

so guys i was watching a code where returns a string to int from a websites server top zone and and servers top zone web sites work with votes if your server has the biggest amount of votes you get the first place in the rank so what i am getting from the site is the votes that the server has as a string ( i found the script in another forum ) and try it myself to test or understand it and i don't understand one thing in this script;

String tozponeURL = "http://l2topzone.com/lineage2/server-info/9974/L2Xerus.html";
        URLConnection con = new URL(tozponeURL).openConnection();
        con.addRequestProperty("User-Agent", "Mozilla/4.76");
        isr = new InputStreamReader(con.getInputStream());
        br = new BufferedReader(isr);

        String line;
        while((line = br.readLine()) != null )
        {
             if (line.contains("<div class=\"rank\"><div class=\"votes2\">Votes:<br>"))
                 {
                    int votes = Integer.valueOf(line.split("<div class=\"rank\"><div class=\"votes2\">Votes:<br>")[1].replace("</div></div>", ""));
                    return votes;
                 }

so what this [1] is in the replace method? i cant understand

please help me! i searched the documentation but can't understand

3
  • 2
    line.split returns an array. [1] accesses the second element of the array. Commented Mar 4, 2014 at 23:08
  • 1
    @AntonH that should be an answer not a comment. Commented Mar 4, 2014 at 23:09
  • @IanRoberts I know, but for small questions like that, I can't be bothered. I've missed so much rep that way ... Commented Mar 4, 2014 at 23:10

2 Answers 2

2

When a string is split, it creates an array holding each token. line[1] is referring to the second token in the String line and then its calling replaceAll() on that particular token.

Sign up to request clarification or add additional context in comments.

Comments

1

The java split method returns a String Array:

line.split("....")

so:

line.split("....")[1] 

is the String at index 1 of the array

3 Comments

so in my case the 0 one is this ? <div class=\"rank\"><div class=\"votes2\">Votes:<br> and the 1 is the next one?
No, it splits the line every time it sees "<div class=\"rank\"><div class=\"votes2\">Votes:<br>". A stupid example would be a String aaa<div class="rank"><div class="votes2">Votes:<br>bbb, and [0] = "aaa" and [1] = "bbb".
No. split() splits this line string around matches of the given regular expression. see documentation here: docs.oracle.com/javase/6/docs/api/java/lang/… @Nick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.