0

i have an input in a file where i have some numbers in the format

106,648|403,481 747,826|369,456 758,122|365,637 503,576|808,710 325,374|402,513

not i want to format the number as 106,648 403,481 747,826 .. and so on but i am unable to do this i have done this so far

public class MidPointSum {

public static void main(String[] args) {

    File file=new File("D:/midpoint.txt");

    try{

        Scanner sc=new Scanner(file);

            while(sc.hasNext()){

                String value=sc.next();

                String getVal[]=value.split("|");
                for(int i=0;i<getVal.length;i++){

                    System.out.println(getVal[i]);
                }

            }

    }catch(FileNotFoundException e){
        System.err.println("File is not Found");
    }catch (Exception e){
        System.err.println("Another Exception");
    }

}

but i am not getting the desired output.Please someone help me ..

4

2 回答 2

2

Java 的 split 使用正则表达式,因此需要双转义管道:

String[] getVal=value.split("\\|");

如果它是一个普通的正则表达式,你无论如何都需要对其进行转义,但在 java中"\"是 string 中的一个特殊字符,由于 and 之类的字符\t\n因此是双重转义。

于 2013-06-01T01:44:27.450 回答
1

|是一个正则表达式元字符,需要转义,试试"\\|"

于 2013-06-01T01:45:31.257 回答