Java .split() 越界

我的代码有问题。


我正在尝试从 .txt 文件中提取频道的名称。我不明白为什么这个方法line.split()会给我一个长度为 0 的数组:


有人可以帮助我吗?


这是文件.txt:


------------[channels.txt]------------


...

#CH id="" tvg-name="Example1" tvg-logo="http... 

#CH id="" tvg-name="Example2" tvg-logo="http...

#CH id="" tvg-name="Example3" tvg-logo="http...

#CH id="" tvg-name="Example4" tvg-logo="http...

...

这是我的代码:


try {

    FileInputStream VOD = new FileInputStream("channels.txt");

    BufferedReader buffer_r = new BufferedReader(new InputStreamReader(VOD));

    String line;

    ArrayList<String> name_channels = new ArrayList<String>();


    while ((line = buffer_r.readLine()) != null ) {

        if (line.startsWith("#")) {

            String[] first_scan = line.split(" tvg-name=\" ", 2);

            String first = first_scan[1];               // <--- out of bounds


            String[] second_scan = first.split(" \"tvg-logo= ", 2);

            String second = second_scan[0];


            name_channels.add(second);


        } else {

            //...           

        }

    }

    for (int i = 0; i < name_channels.size(); i++) {

        System.out.println("Channel: " + name_channels.get(i));

    }

} catch(Exception e) {

    System.out.println(e);

}


心有法竹
浏览 189回答 2
2回答

慕码人8056858

所以你有这样的例子#CH id="" tvg-name="Example1" tvg-logo="http...&nbsp;并试图在这些字符串上拆分" tvg-name=\" "" \"tvg-logo= "这些字符串都不在示例中。附加了一个虚假的空格,第二个开头的空格在错误的位置。修复字符串,这里有一个简洁但完整的程序来演示interface Split {&nbsp; &nbsp; static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String line = "#CH id=\"\" tvg-name=\"Example1\" tvg-logo=\"http...";&nbsp; &nbsp; &nbsp; &nbsp; String[] first_scan = line.split(" tvg-name=\"", 2);&nbsp; &nbsp; &nbsp; &nbsp; String first = first_scan[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// <--- out of bounds&nbsp; &nbsp; &nbsp; &nbsp; String[] second_scan = first.split("\" tvg-logo=", 2);&nbsp; &nbsp; &nbsp; &nbsp; String second = second_scan[0];&nbsp; &nbsp; &nbsp; &nbsp; System.err.println(second);&nbsp; &nbsp; }&nbsp;}当然,如果您有任何以 开头'#'但不匹配的行,您也会遇到类似的问题。使用正则表达式和捕获组可能会更好地完成这类事情。

HUX布斯

最后一个双引号后面有一个空格,tvg-name=\"其中与您的示例中的数据不匹配。当您使用 split withline.split(" tvg-name=\"", 2)时,返回数组中的第一项将是#CH id="",第二部分将是Example1" tvg-logo="http..."如果您想获得 的值,tvg-name=您可以使用带有捕获组的正则表达式,您将使用否定字符类捕获而不是双引号[^"]+tvg-name="([^"]+)"try {&nbsp; &nbsp; FileInputStream VOD = new FileInputStream("channels.txt");&nbsp; &nbsp; BufferedReader buffer_r = new BufferedReader(new InputStreamReader(VOD));&nbsp; &nbsp; String line;&nbsp; &nbsp; ArrayList<String> name_channels = new ArrayList<String>();&nbsp; &nbsp; while((line = buffer_r.readLine()) != null ){&nbsp; &nbsp; &nbsp; &nbsp; if(line.startsWith("#")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String regex = "tvg-name=\"([^\"]+)\"";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Pattern pattern = Pattern.compile(regex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Matcher matcher = pattern.matcher(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (matcher.find()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name_channels.add(matcher.group(1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for(int i = 0; i < name_channels.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Channel: " + name_channels.get(i));&nbsp; &nbsp; }}catch(Exception e){&nbsp; &nbsp; System.out.println(e);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java