0

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.

Jimbok21
  • 5
  • 1
  • 1
    You're comparing strings wrong. See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Dan W Feb 28 '20 at 20:05
  • 1
    You can only use variables in the scope in which you declare them, so nothing can see `z` outside of the if-statement. – azurefrog Feb 28 '20 at 20:06

2 Answers2

0

You should learn about "scopes". If you create z inside the if-condition it is not reachable outside that block. So create it outside the if-condition, then it will work.

String objectType = in.next();
String name = in.next();
int x = in.nextInt();
int y = in.nextInt();
int z;
if("Dog".equals(objectType)) {
    z = in.nextInt();
}
String colour = in.next();
int ID = in.next();

if("Dog".equals(objectType)) {
    Dog newDog = new Dog(name, x, y, z, colour, ID);
}

Also the == operator works only with simple types (int, long, float, boolean) but not with Objects (Integer, Long, Float, String, ...). For objects you must use the equals() method.

The operator == used on objects would compare the identity (which is the address in RAM) of the objects, not their values.

Stefan
  • 1,789
  • 1
  • 11
  • 16
  • Thank you, this has fixed most of my problems but I now I have a red line under z in the newDog part which says that the Variable 'z' might not have been initialized. Any ideas on how to fix this? EDIT: I have set z to equal 0 when it is initialised and it seems to work now. Thank you! – Jimbok21 Feb 28 '20 at 20:21
  • initialize z with 0: ```int z=0;```. However that warning is surely false. – Stefan Feb 28 '20 at 20:33
0

Seems to me to be an exercise in object-oriented programming, i.e. class hierarchies, since class Cat and class Dog have common members. In fact they have the same members and Dog has an extra member, namely z.

Also, your input file is like a CSV file that uses a single space as the field delimiter rather than a comma.

Hence I would create a parent class HousePet, since cats and dogs are both usually house pets, which contains the members common to both Cat and Dog and have both those classes extend class HousePet.

Then you simply read the file line by line. For each line you split it by the single space delimiter. If the line has six parts it is a Cat, otherwise it is a Dog. You call the appropriate constructor depending on the number of parts in the line read.

Since you need to add both Cats and Dogs to the same list, create a List of HousePet.

Here is the code.

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CatsDogs {

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(new File("catsdogs.txt"))) {
            List<HousePet> pets = new ArrayList<>();
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] parts = line.split("\\s");
                HousePet pet;
                if (parts.length == 6) {
                    pet = new Cat(parts[1],
                                  Integer.parseInt(parts[2]),
                                  Integer.parseInt(parts[3]),
                                  parts[4],
                                  Integer.parseInt(parts[5]));
                }
                else {
                    pet = new Dog(parts[1],
                                  Integer.parseInt(parts[2]),
                                  Integer.parseInt(parts[3]),
                                  Integer.parseInt(parts[4]),
                                  parts[5],
                                  Integer.parseInt(parts[6]));
                }
                pets.add(pet);
            }
        }
        catch (IOException xIo) {
            xIo.printStackTrace();
        }
    }
}

abstract class HousePet {
    protected String name;
    protected int x;
    protected int y;
    protected String colour;
    protected int id;

    protected HousePet(String name, int x, int y, String colour, int id) {
        this.name = name;
        this.x = x;
        this.y = y;
        this.colour = colour;
        this.id = id;
    }
}

class Cat extends HousePet {
    public Cat(String name, int x, int y, String colour, int id) {
        super(name, x, y, colour, id);
    }
}

class Dog extends HousePet {
    private int z;

    public Dog(String name, int x, int y, int z, String colour, int id) {
        super(name, x, y, colour, id);
        this.z = z;
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41