3

I'm doing an assignment for uni and am stuck on litterelly one line of code.

public static void setKind(SettlementType type) {
        SettlementType t;
        t = scan.nextLine();
        setKind(t = type);

As you can see I am using a scanner to take in the the SettlementType, however, the IDE keeps saying that I need to change SettlementType to String. I'm new to Java and couldn't find a fix if anyone knows could you perhaps walk me through it?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
mkirito
  • 33
  • 5
  • 1
    Since `scan.nextLine()` returns a String, you can't assign it to an Enum. – Suresh Atta Mar 16 '16 at 12:33
  • It looks like XY problem to me. I see you're missing basics of how programming works(assignments, recursion, method/function invoking). Pleas rephrase your question to tell what you want to achieve. Code you posted is wrong beyond simple answer. – zubergu Mar 16 '16 at 12:40

1 Answers1

4

An each enum has valueOf method that turns String value into enum object.

SettlementType t = SettlementType.valueOf(scan.nextLine());

The IllegalArgumentException exception may be throw if the enum has no constant with the specified name.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • Thank you java gure, you have helped a lot – mkirito Mar 16 '16 at 12:39
  • This solves what exactly? I don't believe @mkirito that your code works like a charm just because you copy pasted this one line. Or does it? – zubergu Mar 16 '16 at 12:44
  • @zubergu, why do you think that this code doesn't work? – Andrew Tobilko Mar 16 '16 at 12:50
  • @zubergu you are correct the code as a whole ddoesent work like a charm however this was the problem that needed to be soved at the given time so i could start solving every other problem within the code. basically i was unsure of how to get the scanner to work with Enums – mkirito Mar 16 '16 at 13:10
  • @AndrewTobilko I'm not saying your piece of code won't work - I was just stating that this doesn't work with OPs code. As it turned out - i was right, and now we'll see follow-up questions to the same problem some time soon. That's all. – zubergu Mar 16 '16 at 13:23