I'm trying to read a file which contains the structure PointID CoordX CoordY, like this:
1 565.0 575.0
2 25.0 185.0
3 345.0 750.0
4 945.0 685.0
5 845.0 655.0
Where PointID should be the ID of that dot.
Then, I have to create a Punto object following this structure:
Punto p1 = new Punto(16.47, 36.10);
Punto p2 = new Punto(16.47, 280.44);
Punto p3 = new Punto(115.09, 92.54);
Punto p4 = new Punto(364.39, 197.37);
I came to this solution:
public ArrayList<Punto> puntosFichero(File fich) throws Exception {
String numPunto = "";
String cordX = "";
String cordY = "";
String tmp = "";
ArrayList<Punto> mapa = new ArrayList();
//Opens the file
try {
scannerLectura = new Scanner(fich);
} catch (Exception e) {
throw new Exception("Error: Abrir fichero");
}
//Reads the file and sets the ArrayList
while(scannerLectura.hasNext()){
tmp = scannerLectura.next();
if (tmp.contentEquals("NODE_COORD_SECTION")){
while(!tmp.contentEquals("EOF")){
numPunto = scannerLectura.next(); //P(numPunto)
cordX = scannerLectura.next(); //CordX
cordY = scannerLectura.next(); //CordY
System.out.println("Punto " numPunto ": [" cordX " , " cordY "]");
Punto (Object)numPunto = new Punto (Double.parseDouble(cordX), Double.parseDouble(cordY));
mapa.add(Integer.parseInt(numPunto));
}
}
}
//Closes the file
scannerLectura.close();
return mapa;
}
But when I try to create a new Punto here:
Punto (Object)numPunto = new Punto (Double.parseDouble(cordX), Double.parseDouble(cordY));
I can't use the string "numPunto" as its name. Is there a way to do that?
CodePudding user response:
Probably you want his:
Map<Integer,Punto> mapa = new HashMap<>();
// ...
Punto punto = new Punto (Double.parseDouble(cordX), Double.parseDouble(cordY));
mapa.put(Integer.parseInt(numPunto),punto);
Saving Punto
s into a map with the PointID as key.
Or you should show the Punto.class or add an id
field to Punto.
CodePudding user response:
You can not use a dynamic name for the variable (aka one that changes/is decided while runinng the program), the name has to be defined when writing the code. You can either use a map like PeterMmm suggested in his answer and then store the points with some kind of name as key or you can just use a list to store the points and access them by their index inside the list.
List<Punto> puntos = new ArrayList<>(); // could also use different list types, doesnt really matter
...
// for every
puntos.add(<punto you created from the file line>);
// access them like this:
puntos.get(<index>);
Note that indices in java start at 0, so you access the first element by using <list>.get(0);
.
EDIT: I have just realized that you already have a list. What you're currently doing is adding the index to the list instead of the object. What you wanna do is first of all remove the "(Object)" before the punto variable name, this just does not work at all. Then instead of mapa.add(Integer.parseInt(numPunto));
you wanna do mapa.add(numPunto)
which will add the punto object to the list. Finally you can access the objects from the list by doing mapa.get(index), like I already said: the first element will be accessed by using index = 0 as that's just how Java works. So the one that has the "1"-prefix in your file will actually be the one you get with mapa.get(0).