需要理解阅读CSV文件并在Java中使用它们

szqfcxe2  于 11个月前  发布在  Java
关注(0)|答案(1)|浏览(90)

我试图读取一个CSV文件,其中包含GPS坐标(纬度,经度和海拔)以及每个示例的时间戳。格式如下:
2014-08-06T21:02:23Z,-33.7939310,151.0553090,-5.0
这样就可以将每一个数据元素分离成一个way-point(基本上是将每一行放入一个数组),然后将每一个way-point分开,以获得每个数据元素作为其自己的字符串。
我用逗号分隔字符串,给予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) {
    
    
    
    
}

}

字符串

txu3uszq

txu3uszq1#

这个问题的措辞不正确,这会让你每次在这里都被殴打。
你需要做的第一件事是阅读以下内容:https://stackoverflow.com/help/how-to-ask
考虑到这一点,你需要发布你遇到的问题,我假设这是一个ArrayOutOfBoundsException,因为java数组从索引0开始,而不是1。
在那之后,我想你会遇到日期的问题,这很复杂,因为你应该使用日期解析器,而不是上面所说的。
我还假设你知道如何计算两个坐标之间的距离,如果不是,这是一个单独的问题。
你需要开始把事情分解成更面向对象的东西。
大多数学校作业不想让你使用像OpenCsv这样的库,因此你的扫描仪。你可以带着你的扫描仪,假设它为你工作,只是创建一个结构传递给工厂。我建议List<String[]>,每个String数组是CSV文件中的一行。
你应该从类的Angular 来考虑,例如下面的坐标类:

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;
    }
}

字符串
并使用一个helper类(或者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个格式正确的值。
尽你所能,一次测试一个方法,这将帮助你理解发生了什么。

相关问题