1

I have following class:

public class Owner {

  private final Integer id;
  private final OwnerType type;

  public Owner(Integer iId, Enum eType) {
      this.id= iId;
      this.lastName = lName;
      this.type = eType // how should that work?
  } 

}

And

public enum OwnerType {
    HUMAN,INSTITUTION   
}

Which I am calling via:

try {
    File file = new File("resources/Owner.txt");
    Scanner fileContent = new Scanner(file);
    while (fileContent.hasNextLine()) {
            String[] person = fileContent.nextLine().split(" ");
            this.data.add(new Owner(owner[0],owner[1]));
        }
    } catch (FileNotFoundException err){
        System.out.println(err);
    }

Where Owner.txt has a format of:

ID TYPE

Like that:

1 HUMAN
2 INSTITUTION

My question is:

How can I specify the type property of my Owner Object when I am calling the following?

new Owner(owner[0],owner[1]) 
Mureinik
  • 297,002
  • 52
  • 306
  • 350
uksz
  • 18,239
  • 30
  • 94
  • 161
  • what is 'owner' ? what type(s) does it hold? – Stultuske Mar 25 '16 at 11:18
  • 4
    Firstly, can you change the name of `Enum` so that it's *not* the name of a class in the `java.lang` package? It will make both your code and your question much simpler. – Jon Skeet Mar 25 '16 at 11:18
  • Your "constructor" name (`AccountOwner`) doesn't match the class name (`Owner`). – Mena Mar 25 '16 at 11:19
  • 4
    Past that, it sounds like you need `Enum.valueOf` – Jon Skeet Mar 25 '16 at 11:19
  • It looks like duplicate for http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java. By the way, you also need convert `String` to `Integer`. – Artur Zagretdinov Mar 25 '16 at 11:32
  • 1
    Please post code that compiles, or at least, that has a compile error only in the problematic place. There is no variable named `owner`. And whatever array it is, it cannot match the parameters of the constructor. Why are you using `Enum` instead of `OwnerType` anyway? – RealSkeptic Mar 25 '16 at 11:33

2 Answers2

2

There are two issues here.

First, the Owner's constructor should receive an OwnerType, not just any Enum:

public Owner(Integer iId, OwnerType eType) {
    this.id= iId;
    this.type = eType;
} 

When parsing the input file, you could use the valueOf method to convert a string value to an OwnerType:

this.data.add
    (new Owner(Integer.parseInt(owner[0]), OwnerType.valueOf(owner[1])));
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Any Enumeration object have by default the method valueOf(String key), the what this method does is search into all defined values into your enum class and return the right one if find it.

For more info keep on eye on this:

https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#valueOf%28java.lang.Class,%20java.lang.String%29enter link description here

In this particular case the enum;

public enum OwnerType {
    HUMAN,INSTITUTION   
}

if we use OwnerType.valueOf("HUMAN"), will return the enum type HUMAN

Here use this:

try {
    File file = new File("resources/Owner.txt");
    Scanner fileContent = new Scanner(file);
    while (fileContent.hasNextLine()) {
        String[] person = fileContent.nextLine().split(" ");
        this.data.add(new Owner(person[0],OwnerType.valueOf(person[1])));
    }
} catch (FileNotFoundException err){
    System.out.println(err);
}
cralfaro
  • 5,822
  • 3
  • 20
  • 30