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 ..

2
  • while splitting try to escape the pipe character. as .split("\|"); Commented Jun 1, 2013 at 1:43
  • i also want the sum of the y coordinates in this pair ...can anyone help?? Commented Jun 1, 2013 at 2:07

2 Answers 2

2

Java's split uses a regex, so you need to double escape pipe:

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

If it were a plain regex you'd need to escape it anyway, but in java "\" is a special character in a string, due to characters like \t and \n, hence the double escape.

Sign up to request clarification or add additional context in comments.

Comments

1

| is a regex metacharacter and it needs to be escaped, try "\\|"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.