我有一个程序可以使用Jsoup获取特定团队中曲棍球运动员的统计数据并将其放入单个字符串中。我想把这些线分开,这样我就可以用自己的字符串获得每个玩家的统计数据。这是我的代码并解释过:
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);
}
}
}
}
输出给出了多行,让你知道它们是前三行:
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
如果行的开头= Matt Read我想创建一个包含以下内容的字符串:
Matt Read, C:13 GOALS: 5 ASSISTS: 4 POINTS: 9 PLUS/MINUS: 2 PIM: 2
如果行的开头= Brayden Schenn我想创建一个包含以下内容的字符串:
Brayden Schenn, C:12 GOALS: 2 ASSISTS: 6 POINTS: 8 PLUS/MINUS: 3 PIM: 5
答案 0 :(得分:3)
除非我不理解代码,否则你似乎已经这样做了 - 每个循环迭代中每一行都在String a
变量中。您只需要向外保存a
的每个作业。在初始for循环之前实例化ArrayList<String>
,并在致电a
之前向其添加变量System.out.println(a);
。退出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.
}
答案 1 :(得分:1)
您可以使用Scanner类通过调用scanner.nextLine()方法从单个文本体中(或直接从文件中)拉出单独的行。它只会扫描输入字符串,直到遇到一个新的行字符,并将所有内容返回给它。
答案 2 :(得分:1)
您可以使用扫描仪,或者更优选的是List,甚至是HashMap来存储有关播放器的信息。
不是打印该行,而是声明您的首选类型并将字符串存储在那里。