简体   繁体   English

将字符串分成多行

[英]Split a string into multiple lines

I have a program that gets the stats of hockey players on a specific team using Jsoup and puts it in to a single string. 我有一个程序可以使用Jsoup获取特定团队中曲棍球运动员的统计信息,并将其放入单个字符串中。 I want to split the lines up so I can get the stats for each player in their own string. 我想将各行分开,以便我可以在自己的字符串中获取每个球员的统计信息。 This is the code I have and explained: 这是我拥有并解释的代码:

public static void main(String[] args) throws IOException {


    Document doc = Jsoup.connect("http://espn.go.com/nhl/team/stats/_/name/phi/philadelphia-flyers").get();

    for (Element table : doc.select("table.tablehead")) {
        for (Element row : table.select("tr")) {
            Elements tds = row.select("td");
             if (tds.size() > 6) {
                String a = tds.get(0).text() + ":" + tds.get(1).text() + " GOALS: " + tds.get(2).text() + " ASSISTS: " + tds.get(3).text() + " POINTS: " + tds.get(4).text() + " PLUS/MINUS: " + tds.get(5).text() + " PIM: " + tds.get(6).text();
                System.out.println(a);
            }
        }
    }
}

The output gives multiple lines, to give you an idea these are the first three lines it gives: 输出给出多行,让您大致了解以下是它们给出的前三行:

PLAYER:GP GOALS: G ASSISTS: A POINTS: PTS PLUS/MINUS: +/- PIM: PIM
Matt Read, C:13 GOALS: 5 ASSISTS: 4 POINTS: 9 PLUS/MINUS: 2 PIM: 2
Brayden Schenn, C:12 GOALS: 2 ASSISTS: 6 POINTS: 8 PLUS/MINUS: 3 PIM: 5

If the beginning of the line = Matt Read I want to make a string that contains: 如果该行的开头= Matt Read,我想制作一个包含以下内容的字符串:

Matt Read, C:13 GOALS: 5 ASSISTS: 4 POINTS: 9 PLUS/MINUS: 2 PIM: 2

If the beginning of the line = Brayden Schenn I want to make a string that contains: 如果该行的开头= Brayden Schenn,我想制作一个包含以下内容的字符串:

Brayden Schenn, C:12 GOALS: 2 ASSISTS: 6 POINTS: 8 PLUS/MINUS: 3 PIM: 5

Unless I'm not understanding the code, you seem to pretty much already do this - each line is in the String a variable on every loop iteration. 除非我不理解代码,否则您似乎已经做完了-每次循环迭代中,每一行都在String a变量。 You just need to save outward each assignment of a . 你只需要向外省的各分配a Instantiate an ArrayList<String> prior to your initial for loop and add variable a to it prior to your call to System.out.println(a); 在初始for循环之前实例化ArrayList<String> ,并在调用System.out.println(a);之前向其添加变量a System.out.println(a); . After you exit your for loop, your ArrayList should contain each appropriate individual String . 退出for循环后, ArrayList应包含每个适当的单独String

public static void main(String[] args) throws IOException {

    List<String> list = new ArrayList<String>(); // THIS LINE IS NEW

    Document doc = Jsoup.connect("http://espn.go.com/nhl/team/stats/_/name/phi/philadelphia-flyers").get();

    for (Element table : doc.select("table.tablehead")) {
        for (Element row : table.select("tr")) {
            Elements tds = row.select("td");
             if (tds.size() > 6) {
                String a = tds.get(0).text() + ":" + tds.get(1).text() + " GOALS: " + tds.get(2).text() + " ASSISTS: " + tds.get(3).text() + " POINTS: " + tds.get(4).text() + " PLUS/MINUS: " + tds.get(5).text() + " PIM: " + tds.get(6).text();

                    list.add(a);  // THIS LINE IS NEW

                System.out.println(a);
            }
        }
    }

        // at this point, variable "list" contains each String assigned to "a" above.

}

You can use the Scanner class to pull individual lines out of any body of text (or directly from the file) by calling the scanner.nextLine() method. 您可以通过调用scan.nextLine()方法,使用Scanner类将单个行从任何文本主体中拉出(或直接从文件中拉出)。 It will just scan through the input string until it encounters a new line character, and return everything up to it. 它只会扫描输入字符串,直到遇到换行符,然后返回所有内容。

You could use a scanner, or more preferably a List, or even a HashMap to store the information about the player in. 您可以使用扫描仪,或更优选地使用列表,甚至可以使用HashMap来存储有关播放器的信息。

Instead of printing the line, declare your prefered type and store the string a there. 而不是打印行,而是声明您的首选类型并将字符串存储在此处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM