Home String in for loop doesn't get full context
Reply: 0

String in for loop doesn't get full context

user2089
1#
user2089 Published in July 15, 2018, 9:19 pm

This question already has an answer here:

  • ArrayList of Strings to one single string 7 answers

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313232 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO