Access data from a class

Asked

Viewed 63 times

2

I’m developing a graph generator to monitor the navigation of a robot. The coordinates the robot passes through are stored in a txt file, with each line having a latitude and a longitude, as described in that question. I’m using the @Victorstafusa example

But I’m having trouble accessing the class data Coordenada.

To plot a line on my graph, I use the latitude and longitude of two coordinates. So I need each object to have the coordinates of a txt line. The code I use to plot a line is linha.drawLine(longitudeCoordenada1, latitudeCoordenada1, longitudeCoordenada2, latitudeCoordenada2). I then thought of instantiating objects as follows Coordenada c = new Coordenada(), to use thus: linha.drawLine(c.getLongitude(), c.getLatitude(), c2.getLongitude(), c2.getLatitude()). But I was unsuccessful.

I have little experience in object orientation, so you could tell me how to do so that each object has the coordinate of a txt line and how to access these coordinates?

To test I am using the following main class.

public class Main {

    public static void main(String[] args) throws IOException {
        double longitude1 = 0;
        double latitude1 = 0;
        double longitude2 = 0;
        double latitude2 = 0;

        Leitura l = new Leitura();
        l.lerCoordenadas();

        Coordenada c1 = new Coordenada(longitude1, latitude1);
        Coordenada c2 = new Coordenada(longitude2, latitude2);

        System.out.println(c1.getLongitude());
        System.out.println(c1.getLatitude());
        System.out.println(c2.getLongitude());
        System.out.println(c2.getLatitude());
    }
}
  • Look at that. When doing Coordinate c = new Coordinate() you are instantiating an object but without setting its values, so when you access it with c.getLongitude() the longitude value is null.

  • 1

    @Lucasbrogni In this case not even that. It is build error even because there is no constructor without parameters in this class.

  • "I use the latitude and longitude of two coordinates" - actually the opposite. The first coordinate on your list is -54.123440,-21.123456 and the other coordinates are similar and very close. Interpreted as latitude-longitude, it is a point in the middle of the Atlantic Ocean not far from Antarctica, should not be a place of your interest. Interpreting as longitude-latitude, it is a place about 60km from Campo Grande - MS, which should be what interests you.

  • Thanks for pointing out the error, I edited the question correcting it.

1 answer

2


Use the parameters in the constructor:

double longitude1 = ...;
double latitude1 = ...;
double longitude2 = ...;
double latitude2 = ...;

Coordenada c1 = new Coordenada(longitude1, latitude1);
Coordenada c2 = new Coordenada(longitude2, latitude2);

linha.drawLine(c1.getLongitude(), c1.getLatitude(), c2.getLongitude(), c2.getLatitude());

Edited:

Once you have edited the question, the problem is that you are reading the values of the file and are doing nothing with them. Note that the method lerCoordenadas() returns a List<Coordenada>. However, you are totally ignoring the produced list and instead you create two zero-value coordinates totally independent of the list you read from the file and then show those zero-value coordinates.

Try to do this:

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

public class Main {

    private static List<Coordenada> lerCoordenadas() throws IOException {
        return lerCoordenadas(Paths.get("c:/dados/log.txt"));
    }

    private static List<Coordenada> lerCoordenadas(Path arquivo) throws IOException {
        return Files.readAllLines(arquivo, StandardCharsets.UTF_8)
               .stream()
               .map(Coordenada::parse)
               .collect(Collectors.toList());
    }

    public static void main(String[] args) throws IOException {
        List<Coordenada> coordenadas = lerCoordenadas();

        for (Coordenada c : coordenadas) {    
            System.out.println(c.getLongitude());
            System.out.println(c.getLatitude());
        }
    }
}

The class Coordenada is the same as my answer to your previous question. The class Leitura of the previous question can be discarded, as this class Main then already absorbed its contents.

To draw a polygon with these coordinates, you would do something more or less like this:

public void desenhar(List<Coordenada> coordenadas) {
    Coordenada anterior = null;
    for (Coordenada c : coordenadas) {
        if (anterior != null) {
            linha.drawLine(anterior.getLongitude(), anterior.getLatitude(), c.getLongitude(), c.getLatitude());
        }
        anterior = c;
    }
    Coordenada primeira = coordenadas.get(0);
    linha.drawLine(anterior.getLongitude(), anterior.getLatitude(), primeira.getLongitude(), primeira.getLatitude());
}

Of course, for this to work, it is necessary to know what exactly this object is linha.

  • I edited the question with the main that I’m using, I still can’t use the values of txt

  • @Brunopadilha Reply edited.

  • Now it worked properly, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.