简体   繁体   English

如何划分字符串(x1,y1)(x2,y2)格式

[英]How to divide string (x1,y1)(x2,y2) format

how to divide string (x1,y1)(x2,y2) format 如何划分字符串(x1,y1)(x2,y2)格式

for example String is (100,200) (300,600) and i am creating objects(point) 例如String是(100,200) (300,600) ,我正在创建对象(点)

class point{
    int x;
    int y;
}

i tried using StringTokenizer but this is not the proper way. 我尝试使用StringTokenizer但这不是正确的方法。

I would do something like this - 我会做这样的事情 -

public static class Point {
  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }

  private int x;
  private int y;

  public int getX() {
    return x;
  }

  public void setX(int x) {
    this.x = x;
  }

  public int getY() {
    return y;
  }

  public void setY(int y) {
    this.y = y;
  }

  public String toString() {
    return "(" + String.valueOf(x) + ", "
        + String.valueOf(y) + ")";
  }

  public static Point[] fromString(String in) {
    if (in == null) {
      return null;
    }
    List<Point> al = new ArrayList<Point>();
    int p = 0;
    for (;;) {
      int openPos = in.indexOf('(', p);
      if (openPos > -1) {
        int closePos = in.indexOf(')', openPos + 1);
        if (closePos > -1) {
          String str = in.substring(openPos + 1,
              closePos);
          String[] t = str.split(",");
          p = closePos + 1;
          if (t.length != 2) {
            continue;
          }
          al.add(new Point(Integer.valueOf(t[0]
              .trim()), Integer.valueOf(t[1].trim())));
        } else {
          break;
        }
      } else {
        break;
      }
    }
    return al.toArray(new Point[al.size()]);
  }
}

public static void main(String[] args) {
  Point[] pts = Point
      .fromString("(100,200) (300,600)");
  System.out.println(Arrays.toString(pts));
}

Which, when I run it, outputs - 哪,当我运行它时,输出 -

[(100, 200), (300, 600)]

You can create Point objects from the String as shown below: 您可以从String创建Point对象,如下所示:

public class Sample {
    static List<Point> points = new ArrayList<Point>();

    public static void main(String[] args) {
        String s = "(100,200) (300,600)";
        toPoints(s.replaceAll("[(),]", " "));

        for (Point i : points)
            System.out.println("points = " + i);
    }

    private static void toPoints(final String value) {
        final Scanner scanner;
        scanner = new Scanner(value);
        while (scanner.hasNextInt()) {
            points.add(new Point(scanner.nextInt(), scanner.nextInt()));
        }
    }
}

Trim leading/trailing brackets and split on bracket pairs, then for each x/y set, split on comma: 修剪前导/尾随括号并在括号对上拆分,然后对于每个x / y集,在逗号上拆分:

for (String pair : str.replaceAll("^\\(|\\)$", "").split("\\)\\s*\\(")) {
    int x = Integer.parseInt(pair.split(",")[0]);
    int y = Integer.parseInt(pair.split(",")[1]);
    Point point = new Point(x, y);
    // do something with point
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重新格式化 GetVertices 以返回 (x1,y1,0), (x2,y2,0), (x3,y3,0);? - How to re format GetVertices to return (x1,y1,0), (x2,y2,0), (x3,y3,0);? 按钮无法绘制具有坐标(x1,x2,y1,y2)的折线图 - Button not working to draw line graph having its coordinate (x1, x2, y1, y2) 如果我们知道距离x1,y1,y2,则计算x2 - Calculate x2 if we know distance, x1, y1, y2 如何确定百万数据点中的哪些点 (x,y) 位于矩形 (x1, x2, y1, y2) 所描述的区域内? - How to determine which points (x,y) out of million data points lie inside the area described by a rectangle (x1, x2, y1, y2)? 在java中以给定速度以直线将对象从点(x1,y1)移动到点(x2,y2)的方法 - Method to move an object from point(x1,y1) to point(x2,y2) at a given speed in a straight line in java 如何使用Quartz库每天在XX:XX处安排从日期X1 / Y1 / Z1到日期X2 / Y2 / Z2的某些任务? - How can I use Quartz library to schedule some task from date X1/Y1/Z1 to date X2/Y2/Z2 at XX:XX every day? 在X1 y1 X2 y2形式的10,000个点的文件中,如何检测至少四个正方形? 爪哇 - In a file of 10,000 points of the form X1 y1 X2 y2 ,,, How to detect at least 4 which form a square ? java 给定两个点 A(x1,y1) &amp; B(x2,y2),我想在球面上找到第三点 C(x3,y3) 和线 AB 之间的距离和 AD 的长度 - Given two points A(x1,y1) & B(x2,y2), I want to find the distance between the third point C(x3,y3) and line AB and length of AD on spherical surface 将位图从(x,y)移到(x2,y2)如何计算起始和目标之间的x,y值 - move bitmap from (x,y) to (x2,y2) how to compute x,y values between start and destination 为什么 x *= (y1*y2*y3)/z1 没有给出与 JAVA 中的 x = x * (y1*y2*y3)/z1 相同的答案 - Why is x *= (y1*y2*y3)/z1 not giving the same answer as x = x * (y1*y2*y3)/z1 in JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM