【问题标题】:Need a hand understanding reading CSV files and using them in java需要了解阅读 CSV 文件并在 java 中使用它们
【发布时间】:2014-10-14 01:00:29
【问题描述】:

我正在尝试读取包含 GPS 坐标(纬度、经度和海拔)以及每个实例的时间戳的 CSV 文件。格式如下:

2014-08-06T21:02:23Z, -33.7939310, 151.0553090, -5.0

这样将每个分隔成一个路点(基本上将每一行放入一个数组中),然后将每个路点分开以将每个数据成员作为其自己的字符串。所以只是快速概述一下我到目前为止所做的事情。

我用逗号分隔字符串以提供 4 个单独的字符串,然后使用 parseDouble 将数字字符串更改为双精度字符串。

我认为我的格式是错误的,但我不能 100% 确定,因为我对 java 还是很陌生,最后一种方法将涉及使用“getters”中的数据。

任何帮助将不胜感激,因为我正在努力掌握这个项目!

public class Waypoint {

// The radius of the Earth in km
final double R = 6373.0; 

public double getLatitude(String[] field) {
    double latitude = Double.parseDouble(field[3]);



    return latitude; 
}

public double getLongitude(String[] field) {
    double longitude = Double.parseDouble(field[2]);

    return longitude;

}

public double getElevation(String[] field) {
    double elevation = Double.parseDouble(field[4]);


    return elevation;
}

public String getTimestamp(String[] field) {
    String date = field[1];


    return date;
}

public Waypoint(final String wstring) throws GPSException, FileNotFoundException  {

    Scanner scanner = new Scanner(new FileReader(wstring));

    scanner.hasNextLine();

    while(scanner.hasNextLine()) {
        String waypoint = scanner.nextLine();           
        String[] field = waypoint.split(",");           
    }

}

public double distanceTo(Waypoint wp2) {




}

}

【问题讨论】:

  • 是什么让你觉得有问题?
  • 由于我编写代码的方式,我只是不知道如何计算每个航点之间的距离。

标签: java csv


【解决方案1】:

这个问题的措辞不正确,每次出来都会让你大吃一惊。

您需要做的第一件事是阅读以下内容:https://stackoverflow.com/help/how-to-ask

鉴于此,您需要发布遇到的问题,我假设这是一个 ArrayOutOfBoundsException,因为 java 数组从索引 0 开始,而不是 1。

在那之后,我想您遇到了日期问题,这很复杂,因为您应该使用日期解析器而不是上面所说的。

我还假设您知道如何计算两个坐标之间的距离,如果不是,那是一个单独的问题。

你需要开始把事情分解成更面向对象的东西。

大多数学校作业不希望您使用 OpenCsv 之类的库,因此您需要使用扫描仪。你可以拿你的扫描仪,假设它对你有用,然后创建一个结构传递给工厂。我建议List<String[]>,每个字符串数组都是 CSV 文件中的一行。

你应该考虑类,例如如下的坐标类:

package com.techtrip.labs.stackoverflow;

import java.time.ZonedDateTime;

public class Coordinate {

    private ZonedDateTime timeStamp;
    private Double latitude;
    private Double longitude;
    private Double elevation;

    public Coordinate() {
        super();
    }

    public ZonedDateTime getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(ZonedDateTime timeStamp) {
        this.timeStamp = timeStamp;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public Double getElevation() {
        return elevation;
    }

    public void setElevation(Double elevation) {
        this.elevation = elevation;
    }
}

并使用辅助类(或者可能是 Java 8 中具有默认方法的接口)

package com.techtrip.labs.stackoverflow;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

public class CoordinateFactory {

    public List<Coordinate> getCoordinates(List<String[]> raw) {
        List<Coordinate> coordinates = new ArrayList<Coordinate>(raw.size());

        for (String[] row : raw) {
            coordinates.add(createCoordinate(row));
        }

        return coordinates;

    }

    private Coordinate createCoordinate(String... args) {
        // add error checking here as an assignment
        Coordinate coordinate = new Coordinate();

        coordinate.setTimeStamp(parseToLocalDateTime(args[0]));
        coordinate.setLongitude(Double.parseDouble(args[1]));
        coordinate.setLatitude(Double.parseDouble(args[2]));
        coordinate.setElevation(Double.parseDouble(args[3]));
        return coordinate;
    }

    private ZonedDateTime parseToLocalDateTime(String timeStamp) {
        // SEE Java 8 API Documentation on how to
        // Parse your date into a proper date time format

        /*
         * Let as assignment for ops
         * http://docs.oracle.com/javase/8/docs/api/java
         * /time/format/DateTimeFormatter.html
         * http://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
         */

        return null;
    }
}

最后写一个方法来计算两个坐标之间的距离,我做最后的假设是你有方便的算法。您可以将以下内容放在主目录中。

public static Double distanceBetween(Coordinate c1, Coordinate c2){

    // This should be your starting point
    return null; // replace null with the value
}

所有这些都应该处理你的大部分结构性内容。一个完整的程序应该有错误检查,例如确保每行有 4 个格式正确的值。

以任何方式一次测试每种方法。它将帮助您了解正在发生的事情。

【讨论】:

    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 2021-05-04
    相关资源
    最近更新 更多