-1

Since I'm a beginner I have no clue about the following problem, I've did hours of research but nothing could help me.
I have declared a static string which will be assigned in a for loop, later the string get's printed out in the console but it doesn't contain the full context.

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class dothework {

    static String info;

    public void scrapeinfo() throws IOException {

        Document doc = Jsoup.connect("html").get();

        Elements el_name = doc.select(".champ-name");
        Elements el_speed = doc.select(".speed");
        Elements el_altitude = doc.select(".altitude");

        el_name.size();
        el_speed.size();
        el_altitude.size();
        for(int i = 0; i < 5; i++) {
            String name = el_name.get(i).text();
            String speed = el_speed.get(i).text();
            String altitude = el_altitude.get(i).text();
            String tempinfo = (name+": "+speed+" ("+altitude+")");
            System.out.println(tempinfo);
            info = tempinfo;
        }
    }

    public void printinfo() {
        System.out.println(info);
    }
}

While System.out.println(tempinfo); prints out the full information of 5 rows each containing name, speed and altitude the other System.out.println(info); prints out just one row with name, speed and altitude. So the problem is that the strings differ even though I equalized them here:info = tempinfo;. I guess that has something to do with the for-loop and/or with the modifier but I can't figure it out. Please help me.

John Dhoe
  • 1
  • 2
  • Are you asking about the difference between `info = tempinfo;` and `info += tempinfo;`? – Hovercraft Full Of Eels Jan 13 '18 at 01:06
  • You're missing a main method to say this is a [mcve], but remove the static on the string – OneCricketeer Jan 13 '18 at 01:06
  • I think OP just wants to have all 5 lines that get printed in the loop to the String `info` and also have it print in 5 lines... as in add each `tempinfo` followed by a newline to `info`... `info += tempinfo + "\n"` – DigitalNinja Jan 13 '18 at 01:10
  • Yeah, he's throwing out the previous information each time he sets info to refer to a new tempinfo String. He wants to do `info += tempinfo + "\n";` or some such. Not a very clear question. – Hovercraft Full Of Eels Jan 13 '18 at 01:12
  • @HovercraftFullOfEels I'm asking why the output of those two strings differ? And how is it possible that the output of info really equals the output of tempinfo? I'm sorry that I didn't really asked this... – John Dhoe Jan 13 '18 at 01:13
  • @JohnDhoe: it's only doing what you tell it to do. When you call `info = tempinfo;` you're throwing away any String that into was previously holding. At the end of your loop, info only holds the value from the *last* tempinfo. – Hovercraft Full Of Eels Jan 13 '18 at 01:13
  • @JohnDhoe `info` is getting replaced by `tempinfo` on each loop iteration which leaves you with only the last `tempinfo` in `info` when you call `printinfo`. – DigitalNinja Jan 13 '18 at 01:14
  • `info += tempinfo + "\n";` - this did the math. I'm really thankful! – John Dhoe Jan 13 '18 at 01:17

1 Answers1

0

You should be using a StringBuilder to accumulate the lines.

public class Dothework {

    StringBuilder info = new StringBuilder();

    // for... 

        info.append(tempinfo).append("\n");

Currently, each iteration of the loop overwrites the previous value stored in the string. You also don't need a static variable here.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245