0

I'm being completely beaten up by types in 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.

Each line of my txt file looks like so:

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;
    }
3
  • data.get(i).split(",") will return String[], you have to use index in order to get each token, access using index like this data.get(i).split(",")[0], data.get(i).split(",")[1]; Commented Apr 12, 2018 at 1:32
  • or else declare your 'data' like this List<String[]> data = new ArrayList<String[]>(); Commented Apr 12, 2018 at 1:39
  • As other answer mentioned, the compilation error is simply due to you are adding of split() (which is a String[]) to a List<String>. However, your code shows a lot more problems. e.g. What you are returning is not making sense (returning String[], but you mentioned you want to parse the coordinates to double); A lot of unrelated code is included in the question etc. Commented Apr 12, 2018 at 3:15

2 Answers 2

1

The split(",") method return array of string string[] and you can't set string by array of 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:

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;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Amin - I'd actually used the Point class elsewhere in my program but your solution makes more sense. However, when I try and implement it I get the error - Non-static variable cannot be referenced from a static context, on the line of point.add(new...
Your welcome @scott_ this error because you try to use point class as inner class and if it be inner can't use it in static method of that class,
@scott_ the point class should not be in class that getFileContents method be in it
1

you can update your while loop like this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.