So I have a method that reads a file and assigns classes to elements of an array. How do I assign a special character for each class that I am giving to my array?
The array is of the class “Element” that has 3 attributes (int, int, char)
and those classes (Fantasma, which is a subclass of “Element”).
public void ReadFile() throws FileNotFoundException
{
Scanner scan = new Scanner(new File("inicio.txt"));
while (scan.hasNext())
{
String line = scan.next();
if (line.equals("Pared"))
{
int i = scan.nextInt();
int j = scan.nextInt();
_mundo = new Pared[i][j];
}
else if (line.equals("Fantasma"))
{
int i = scan.nextInt();
int j = scan.nextInt();
_mundo = new Fantasma[i][j];
}
}
}
It’s not good style to update global variables like your
_mundo. You should have your method return an array.I’m not sure why you would want to duplicate the i / j information as locations in your array and as arguments to your element constructor. It would make more sense to do something like this:
Then you can for example print it out with