读取由逗号和空格分隔的一行 - java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15352627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:19:47  来源:igfitidea点击:

reading one line delimited by both comma and space - java

javajava.util.scanner

提问by Bpache

I am willing to read a series of number from one line like below in Java:

我愿意在 Java 中从如下一行中读取一系列数字:

1   80,982  163,8164                    170,2620    

So eventually I want the result array [1, 80, 982, 163, 8164, 170, 2620]

所以最终我想要结果数组 [1, 80, 982, 163, 8164, 170, 2620]

I am thinking about using scanner, but not clear how to implement it in a neat way, can I ask some tips please?

我正在考虑使用扫描仪,但不清楚如何以简洁的方式实现它,我可以问一些提示吗?

Thanks for reading!

谢谢阅读!

回答by Mark Peters

If it were me, I'd read one line using BufferedReaderand then simply use

如果是我,我会阅读一行BufferedReader,然后简单地使用

String[] values = line.split("[, ]");

I usually find Scanneruntidy and overkill.

我通常觉得Scanner不整洁和矫枉过正。

回答by ktm5124

Yes the Scanner class works well with this.

是的, Scanner 类可以很好地处理这个问题。

public static void main(String[] args) throws Exception {
    Scanner s = new Scanner(strOrFile).useDelimiter(",| ");
    List<Integer> myList = new ArrayList<Integer>();
    while (s.hasNext()) {
       myList.add(s.nextInt());
    }
}

It works especially well if you're reading from a file. (Cuts down the work for you.)

如果您从文件中读取,它的效果特别好。(为您减少工作量。)

回答by rdcrng

Scanner is fine, just set the desired delimiter pattern, see this.

扫描仪很好,只需设置所需的分隔符模式,请参阅

回答by Evgeniy Dorofeev

Since you want String[] the best solution is String.split

因为你想要 String[] 最好的解决方案是 String.split

String[] s = str.split("[ ,]+");

回答by Rais Alam

Split your string with below regex.

用下面的正则表达式拆分你的字符串。

 String input = "1   80,982  163,8164                    170,2620 "; 
        String[] values = input.split("\s*(,|\s)\s*");
        for(String val: values)
        {
            System.out.println(val);

        }

I matches following criteria.

我符合以下条件。

  1. Zero or more white space (,|\\s). If you have more that one separator you can include in bracket like (&|,|@|\\s).
  2. Comma and white space.
  1. 零个或多个空白(,|\\s)。如果您有多个分隔符,您可以将其包含在括号中,例如(&|,|@|\\s).
  2. 逗号和空格。

Check the fiddle here.

检查小提琴here。

Java program

Java程序

回答by Achintya Jha

try this:

试试这个:

public static void main(String[] args) {
    String str = "1 80,982 163,8164 170,2620";
    String[] s = str.split(" |,");
    for(String ss:s)
        System.out.println(ss);
}

回答by Sanath

    String ss = "1 80,982 163,8164 170,2620";
    ss = ss.replaceAll(" ", ",");
    String arr[] = ss.split(",");
    for (int i=0;i<arr.length;i++) {
        System.out.println("line "+arr[i]);
    }

回答by MantaMan

For a simple input file with some commas, spaces, and commas with spaces, then regex of Rais and that of Evgeniy both worked with split. In the latter case, the + sign is critical. None of the other options worked for this data: 1.0 2.0 3.0 4.0 5.0,6.0 7.0 8.0 9.0 10.0, 11.0 12.0

对于带有一些逗号、空格和带有空格的逗号的简单输入文件,Rais 的正则表达式和 Evgeniy 的正则表达式都使用 split。在后一种情况下,+ 号至关重要。其他选项均不适用于此数据:1.0 2.0 3.0 4.0 5.0,6.0 7.0 8.0 9.0 10.0, 11.0 12.0