lqdc
lqdc

Reputation: 531

Delimiters in Java

I am trying to figure out how to use delimiters in Java. Basically, I am trying to parse input like this: "(x1, x2)" into double values. When I assign variables to the next double, the next integer gets counted as a double instead of the value between delimiters. How would I separate the values between delimiters and put them into variables?

    System.out.print("Enter coordinates for two points as (x1, x2) (y1, y2): ");
    Scanner input = new Scanner (System.in);
    String wholeString = input.nextLine();
    Scanner stringScanner = new Scanner (wholeString).useDelimiter("[,\\s\\(\\)]*");
    x1 = stringScanner.nextDouble();
    x2 = stringScanner.nextDouble();
    y1 = stringScanner.nextDouble();
    y2 = stringScanner.nextDouble();
    slope = (y2 - y1) / (x2 - x1);

Upvotes: 1

Views: 12201

Answers (1)

Sergey Vedernikov
Sergey Vedernikov

Reputation: 7744

try to use this delimiter: [,\\s\\(\\)]+

* means 0 or more

Upvotes: 2

Related Questions