简体   繁体   English

不兼容的类型:String []无法转换为String

[英]incompatible types: String[] cannot be converted to String

I'm being completely beaten up by types in Java. 我被Java中的类型完全殴打了。

I have coordinates in a txt file, which ultimately I want to format into an array of these co-ordinates, with each array item being a double. 我在txt文件中有坐标,最终我想将其格式化为这些坐标的数组,每个数组项均为double。

Each line of my txt file looks like so: 我的txt文件的每一行如下所示:

13.716              , 6.576600074768066  

Currently, I'm trying to split this line into an array of two Strings, which I will then try and parse into doubles, but I keep getting the error in the title. 当前,我正在尝试将此行拆分为两个字符串的数组,然后将其尝试解析为双精度型,但是标题中始终出现错误。 Where am I going wrong? 我要去哪里错了?

Any other better approaches on converting my Arraylist of Strings to a formatted list of double coordinates would be great, like so 将我的字符串数组列表转换为双坐标格式列表的任何其他更好的方法都很好,就像这样

[[0,1], [0,2], 0,4]

Code: 码:

 public static String[] getFileContents(String path) throws FileNotFoundException, IOException
    {
        InputStream in = new FileInputStream(new File(path));
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        List<String> data = new ArrayList<String>();
        StringBuilder out = new StringBuilder();
        String line;
//        Skips 1376 characters before accessing data
        reader.skip(1378);
        while ((line = reader.readLine()) != null) {            
                data.add(line);
//                System.out.println(line);
        }  

        for (int i=0; i < data.size(); i++){
            data.set(i, data.get(i).split(","));
        }
//        String[] dataArr = data.toArray(new String[data.size()]);
//        Test that dataArr[0] is correct
//        System.out.println(data.size()); 
//        List<String> formattedData = new ArrayList<String>();
//        for (int i = 0; i < data.size(); i++){
//            formattedData.add(dataArr[i].split(","));
//        }
        reader.close(); 
        return dataArr;
    }

The split(",") method return array of string string[] and you can't set string by array of string. split(",")方法返回字符串string[]数组,您不能按字符串数组设置字符串。

Crate point class with let lan double variabels and then create array of this point and them fill them with data from reading each line: let lan double variabels板条箱的点数let lan double variabels ,然后创建该点的数组,并用读取每一行的数据填充它们:

class Point{
    double lat;
    double len;
    Point(double lat, double len){
        this.lat = lat;
        this.len = len;
    }
}

And then use this class in your code: 然后在您的代码中使用此类:

 public static String[] getFileContents(String path) throws FileNotFoundException, IOException
    {
        InputStream in = new FileInputStream(new File(path));
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        List<String> data = new ArrayList<String>();
        StringBuilder out = new StringBuilder();
        String line;
//        Skips 1376 characters before accessing data
        reader.skip(1378);
        while ((line = reader.readLine()) != null) {            
                data.add(line);
//                System.out.println(line);
        }  
        List<Point> points = new ArrayList<Point>();
        for (int i=0; i < data.size(); i++){
            double lat =  Double.parseDouble(data.get(i).split(",")[0]);

            double len =  Double.parseDouble(data.get(i).split(",")[1]);
            points.add(new Point(lat, len));
            //data.set(i, data.get(i).split(","));
        }
//        String[] dataArr = data.toArray(new String[data.size()]);
//        Test that dataArr[0] is correct
//        System.out.println(data.size()); 
//        List<String> formattedData = new ArrayList<String>();
//        for (int i = 0; i < data.size(); i++){
//            formattedData.add(dataArr[i].split(","));
//        }
        reader.close(); 
        return dataArr;
    }

you can update your while loop like this 您可以像这样更新while循环

while ((line = reader.readLine()) != null) {
    String[] splits = line.split(",");
    for(String s : splits) {
        data.add(s);
    }
}

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

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