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]) 
6
  • what is 'owner' ? what type(s) does it hold? Commented Mar 25, 2016 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. Commented Mar 25, 2016 at 11:18
  • Your "constructor" name (AccountOwner) doesn't match the class name (Owner). Commented Mar 25, 2016 at 11:19
  • 4
    Past that, it sounds like you need Enum.valueOf Commented Mar 25, 2016 at 11:19
  • 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? Commented Mar 25, 2016 at 11:33

2 Answers 2

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])));
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Please explain your answer. Code dumps without explanation are not useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.