So I have a text file that has lots of dogs and cats in it. It looks something like this:
Cat Bones 60 150 Orange 3006
Dog Bruce 170 200 210 White 1530
Dog Bones 130 145 200 Black 0864
Cat Mog 70 140 Black 6037
...
As you can see, all dogs will have an additional int assigned to them where cats do not. This is the same for all dogs and cats in the list. I want to make objects for each dog and cat and store them in the same array. I want to do this using a scanner and by using the in.next() function and variations of it such as in.nextInt(). The main problem that I have is that I cannot tell which line is a dog and which is a cat so it's causing problems with my in.next(). Dogs have 1 more integer than cats so I have to find a way to identify the dogs from the cats and then set the variables to be passed into the constructor for dog/cat.
The dog constructor looks like this
Public Dog(String name, int x, int y, int z, String colour, int ID) {...}
The cat constructor looks like this
Public Cat(String name, int x, int y, String colour, int ID) {...}
I tried to do
String objectType = in.next();
String name = in.next();
int x = in.nextInt();
int y = in.nextInt();
if(objectType == "Dog") {
int z = in.nextInt();
}
String colour = in.next();
int ID = in.next();
if(objectType == "Dog") {
Dog newDog = new Dog(name, x, y, z, colour, ID);
This does not work. The z is in red and 'cannot resolve symbol z'. Any advice would be much appreciated.