无法打印一行中的最终数据

Cannot print the final data in a line

我有一个问题 here.I 需要打开一个文本文件并根据我的模式打印输出 want.I 使用正则表达式模式方法来获得所需的 pattern.I wan remove花括号、逗号和 brackets.I 设法做到了,但我不知道为什么我的代码没有打印出最后一行..

文本文件中的数据是这样的

[{8}    ,    {11}    ,    {19}   ,    {21}     ,     {34,
20,
33,
26,
17,
35,
36,
49,
4,
48,
39,
1,
10,
41,
14,
9,
7,
16,
46,
45,
29,
3,
44,
43,
38,
37,
13,
22,
23,
47,
25,
40,
2,
5,
15,
18,
30,
28,
42,
50,
12,
32,
24,
6,
31,
27}]

我的代码如下

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.swing.JFileChooser;


public class ClusterLine {
private static File f;

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



    String sentence = "";
    try {


        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        f = chooser.getSelectedFile();
        String filename = f.getAbsolutePath();
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);

        sentence = br.readLine();





       Pattern p = Pattern.compile("\{(.*?)\}");
        Matcher m = p.matcher(sentence);
        while (m.find()) {

           System.out.println(m.group(0).replace(",", "").replaceAll("\{", "").replaceAll("\}", ""));
        }

如果使用上面的代码,我应该得到这样的输出

8

11

19

21

34 20 33 26 17 35 36 49 4 48 39 1 10 41 14 7 16 46

但我的问题是为什么我不能得到上面那样的输出?最后一行是missing.I只能得到这样的输出。

8

11

19

21

我用的方法有问题吗?希望有人能帮助我..Tq

您可以进行如下操作:

  1. 分裂于 ,。所以你会得到一个数组。
  2. 在数组的每个元素上删除花括号。
  3. 运行循环。

并且在循环中有如下伪代码:

bw.write(arra[i]);
bw.newLine();

您可以使用模式匹配来做到这一点。

这里是解决方案:

public static void main(String[] args) {
    String newString = "[{124}, {126}, {12, 14}, {13, 18, 130, 113}]";
    Pattern p = Pattern.compile("\{(.*?)\}");
    Matcher m = p.matcher(newString);
    while(m.find())
    {
        System.out.println(m.group(1).replace(",", ""));
    }
}

输出将是:

124
126
12 14
13 18 130 113

已添加

对我来说,下面的代码符合预期 return:

    public static void main(String[] args) throws ParseException {
        String newString = "[{8} , {11} , {19} , {21} , {34, 20, 33, 26, 17, 35, 36, 49, 4, 48, 39, 1, 10, 41, 14, 9, 7, 16, 46, 45, 29, 3, 44, 43, 38, 37, 13, 22, 23, 47, 25, 40, 2, 5, 15, 18, 30, 28, 42, 50, 12, 32, 24, 6, 31, 27}]";
        Pattern p = Pattern.compile("\{(.*?)\}");
        Matcher m = p.matcher(newString);
        while(m.find())
        {
            System.out.println(m.group(1).replace(",", ""));
        }
}

输出将是:

8
11
19
21
34 20 33 26 17 35 36 49 4 48 39 1 10 41 14 9 7 16 46 45 29 3 44 43 38 37 13 22 23 47 25 40 2 5 15 18 30 28 42 50 12 32 24 6 31 27

您应该检查变量:

sentence = br.readLine();

添加 2

您的文件中有换行符。所以你应该从你的字符串中删除它:

newString = newString.replace("\n", "").replace("\r", "");
Pattern p = Pattern.compile("\{(.*?)\}");
Matcher m = p.matcher(newString);
while(m.find()){
    System.out.println(m.group(1).replace(",", " "));
}