Java - 使用扫描器读取特定类型

Java - Using scanner to read in specific type

public void loadTill() throws IOException {
    Scanner infile = new Scanner(new FileReader(SHOP_TILL_DATA_FILE));


    int tillSize = infile.nextInt();
    for (int i = 0;i<tillSize*2;i++){


    UKDenomination ct =infile.next();
    int nc = infile.nextInt();

    DenominationFloat m = new DenominationFloat(ct, nc);

    till.addFloat(m);
    }

}

我希望 ct 是 UKDenomination 类型,我是 java 的新手,还没有完全掌握....

注意:英国面额是一个枚举

有多种声明UKdenomination的方法如下所示 1)

public static UKDenomination fromString(String strValue)

2)

private UKDenomination(int v) {
        value = v;
    }

3)

public enum UKDenomination {

您必须实例化您的对象,然后使用您的方法填充它,请尝试以下操作:

UKDenomination ct = new UKDenomination();
ct = ct.fromString( infile.next());

编辑:

如果是枚举,那么你不能将输入保存为这个枚举的新类型,因为引用Oracle Enum Docs只有一个构造函数,我们不能使用它(它只被使用由编译器):

protected Enum(String name, int ordinal)

Sole constructor. Programmers cannot invoke this constructor. It is for use by code emitted by the compiler in response to enum type declarations. Parameters:

name - - The name of this enum constant, which is the identifier used to declare it.

ordinal - - The ordinal of this enumeration constant (its position in the enum declaration, where the initial constant is assigned an ordinal of zero).