简体   繁体   English

拆分坐标从文件Java读入

[英]Splitting Coordinates Read in from File Java

I'm having trouble splitting a line read in from a txt file in to two points. 我无法将读入的行从txt文件拆分为两点。 The file has a list of coordinates like this: 该文件有一个坐标列表,如下所示:

(0, 0) (1, 2)
(0, 1) (-3, 4)
....

What I'm trying to do is separate them into two separate points so that I can calculate the distance between them. 我想要做的是将它们分成两个独立的点,以便我可以计算它们之间的距离。 The issue I have is splitting them into the points after that I think I have everything correct. 我遇到的问题是将它们分成几个点后我认为我的一切都是正确的。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Here's my code: 这是我的代码:

public static void main(String[] args) {

    File file = new File(args[0]);
    BufferedReader br;
    try {
        br = new BufferedReader(new FileReader(file));
        String line;
        String point1, point2;
        int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
        while((line = br.readLine()) != null) {
            point1 = line.substring(0, line.indexOf(")"));
            point2 = line.substring(line.indexOf(")"), line.length());

            x1 = Integer.parseInt(point1.substring(1, point1.indexOf(",")));
            y1 = Integer.parseInt(point1.substring(point1.indexOf(",") + 2, point1.length() - 1));

            x2 = Integer.parseInt(point2.substring(2, point2.indexOf(",")));
            y2 = Integer.parseInt(point1.substring(point2.indexOf(",") + 2, point2.length() - 1));

            double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
            System.out.println((int)distance);
        }
        System.exit(0);
    } catch(IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }

}

You can try something like this I think. 我想你可以试试这样的东西。 Not very efficient algorithm though. 虽然不是很有效的算法。

public class Main {
public static void main(String[] args) {
    String s = "(0, 0) (1, 2)";
    String[] rawCoords = s.split("\\) \\(");
    Point p1 = parsePoint(rawCoords[0]);
    Point p2 = parsePoint(rawCoords[1]);
    System.out.println(p1.distance(p2));

}

private static Point parsePoint(String s) {
    //remove all brackets and white spaces and split by comma
    String[] rawXY = s.replaceAll("[\\)\\(\\s]", "").split(",");
    return new Point(Integer.parseInt(rawXY[0]), Integer.parseInt(rawXY[1]));
}

public static class Point {
    private final int x;
    private final int y;

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

    public double distance(Point another) {
        return Math.sqrt(Math.pow(x - another.x, 2) + Math.pow(y - another.y, 2));
    }

    @Override
    public String toString() {
        return "Point{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}

} }

You could use Tokenizer 你可以使用Tokenizer

(From the API) The string tokenizer class allows an application to break a string into tokens (来自API)字符串tokenizer类允许应用程序将字符串分解为标记

Read more: http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html 阅读更多: http//docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM