如何读取文本文件并指定它在 Java 中的对象类型?

How can I read a text file and specify what type of object it is in Java?

所以我有一个文本文件,里面有很多猫狗。它看起来像这样:

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
...

如您所见,所有狗都将分配给它们一个额外的 int,而猫则没有。这对列表中的所有狗和猫都是一样的。我想为每只狗和猫制作对象并将它们存储在同一个数组中。我想使用扫描仪并使用 in.next() 函数及其变体(例如 in.nextInt())来执行此操作。我遇到的主要问题是我无法分辨哪条线是狗,哪条线是猫,所以它导致我的 in.next() 出现问题。狗比猫多 1 个整数,所以我必须找到一种方法来识别狗和猫,然后设置要传递给 dog/cat.

的构造函数的变量

dog 构造器看起来像这样

Public Dog(String name, int x, int y, int z, String colour, int ID) {...}

cat 构造函数如下所示

Public Cat(String name, int x, int y, String colour, int ID) {...}

我试过

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);

这不起作用。 z 为红色且 'cannot resolve symbol z'。任何建议将不胜感激。

你应该了解 "scopes"。如果您在 if 条件内创建 z,则在该块之外无法访问它。所以在 if 条件之外创建它,然后它就会起作用。

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);
}

此外,== 运算符仅适用于简单类型(int、long、float、boolean),不适用于对象(Integer、Long、Float、String 等)。对于对象,您必须使用 equals() 方法。

对象上使用的运算符 == 会比较对象的标识(即 RAM 中的地址),而不是它们的值。

在我看来是面向对象编程的练习,即 class 层次结构,因为 class Cat 和 class Dog 有共同点成员。事实上他们有相同的成员并且 Dog 多了一个成员,即 z.

此外,您的输入文件类似于 CSV 文件,它使用单个 space 作为字段分隔符而不是逗号。

因此我会创建一个父项 class HousePet,因为猫和狗通常都是家养宠物,其中包含 CatDog 的共同成员并让这两个 classes 扩展 class HousePet.

然后您只需逐行读取文件即可。对于每一行,您都用单个 space 分隔符将其拆分。如果该行有六个部分,则为 Cat,否则为 Dog。您可以根据读取的行中的部分数量调用适当的构造函数。

由于您需要将 CatDog 添加到同一个列表中,因此创建 HousePetList

这是代码。

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;
    }
}