2 Replies - 264 Views - Last Post: 18 December 2013 - 05:26 PM Rate Topic: -----

#1 pineapplePie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-January 12

How to cascade a string to Enum type?

Posted 18 December 2013 - 09:23 AM

Hi all,

I'm trying to create an object(class Person) based on information read from a text file.
The file has following lines:

#name(String), id(String), occupation(Enum)
Jack, u12345, Student
Edward, u67890, Teacher

I can get each data such as Jack, u12345, and Student using StringTokenizer but the problem I dont know how to cascade Student, Teacher data to Enum type.

This is what Ive done so far
BufferedReader br = new BufferedReader(new InputStreamReader(
					getClass().getResourceAsStream("file.txt")));
			String line;
			while ((line = br.readLine()) != null) {
				StringTokenizer st = new StringTokenizer(line, " ,");
				String name = st.nextToken();
				String id = st.nextToken();
				//Type type = ElementType.valueOf(enumType, st.nextToken());???



Any suggestions would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: How to cascade a string to Enum type?

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,653
  • Joined: 20-September 08

Re: How to cascade a string to Enum type?

Posted 18 December 2013 - 10:44 AM

Person p = new Person(name, id, type.equals("Student")? PersonType.Student : PersonType.Teacher);
is the kind of thing you could do.

btw StringTokenizer is deprecated. Use String.split instead
Was This Post Helpful? 3
  • +
  • -

#3 pineapplePie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-January 12

Re: How to cascade a string to Enum type?

Posted 18 December 2013 - 05:26 PM

Thank you very much. Everything is working fine now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1