participate


Java Programming - converting an object into an String array
<<   Back to Forum  |   Give us Feedback
This topic has 4 replies on 1 page.
Surrounded
Posts:19
Registered: 10/12/09
converting an object into an String array   
Nov 6, 2009 9:15 PM

 
Hi friends,

I have defined one class

  public class UserRegistration {
    String first_name = null;
    String last_name = null;
    String dobd = null;
    String dobm = null;
    String doby = null;
    String gender = null;
    String occupation = null;
    String marital_status = null;
 
 
public String getFirst_name()
    {
       return this.first_name;
    }
   public String getLast_name()
    {
       return this.last_name;
    }
   public String getDobd()
    {
       return this.dobd;
    }
   public String getDobm()
    {
       return this.dobm;
    }
   public String getDoby()
    {
       return this.doby;
    }
 
   ..................
   ....................
} 



and I have instantiated this class in a different class but so far I can access fields of UserRegistration class like this way
UserRegistration object = new UserRegistration();

String str = object.first_name;

It shows that I cant use this object as an aaray like
String str = object[0] ;

My intention is to use this UserRegistration's object as an array so that I can iterate through its fields using a loop not by using its fields name and a dot operator manually.

How to convert this object to and String array?
 
pbrockway2
Posts:7,198
Registered: 4/17/05
Re: converting an object into an String array   
Nov 6, 2009 10:04 PM (reply 1 of 4)  (In reply to original post )

 
My intention is to use this UserRegistration's object as an array so that I can iterate through its fields using a loop not by using its fields name and a dot operator manually.

Why? Or, to put it another way, why provide the getXXX() methods if you don't intend to use them? (*)

Associated with this is the observation that these fields model characteristics of the registration which are manifestly not strings. The dob? for instance are numeric. (but, better, dob is a date). Of course it you treat the fields as what they are (string/int/date/boolean etc) you might have to let go of the "array" business. The positive effect of this is that a Date instance has useful behaviour not shared by a triple of ints (much less of String instances).

If you really want such an array, provide another method in your class which creates an array of the appropriate length, populates it with the right values, and returns it.

[edit]

"so that I can iterate through its fields using a loop": I've just read more carefully. Still, why the for loop? Java!=JavaScript, as they say, and you would be looping over things of radically different types.
 
paulcw
Posts:17,104
Registered: 2/22/00
Re: converting an object into an String array   
Nov 7, 2009 6:19 PM (reply 2 of 4)  (In reply to original post )

 
1) It sounds to me like you don't know about toString, and you're trying to duplicate its behavior in a really bad, awkward way.
Please consider implementing a toString method instead of whatever it is you're trying to do.

2) you should make the fields private.

3) you should possibly also get rid of all those getters. What is this object supposed to do?
 
YoungWinston
Posts:1,220
Registered: 2/19/07
Re: converting an object into an String array   
Nov 9, 2009 3:57 AM (reply 3 of 4)  (In reply to #1 )

 
pbrockway2 wrote:
My intention is to use this UserRegistration's object as an array so that I can iterate through its fields using a loop not by using its fields name and a dot operator manually.

Why? Or, to put it another way, why provide the getXXX() methods if you don't intend to use them? (*)
Amen.
Associated with this is the observation that these fields model characteristics of the registration which are manifestly not strings. The dob? for instance are numeric. (but, better, dob is a date). Of course it you treat the fields as what they are (string/int/date/boolean etc) you might have to let go of the "array" business. The positive effect of this is that a Date instance has useful behaviour not shared by a triple of ints (much less of String instances).
In addition, I'd suggest that 'gender' and 'marital_status' should probably be enums, and 'occupation' might well warrant a whole new class. What if somebody decided to put the string "green" in your 'gender' field, for example?

@OP: Like paulcw, I think you may be confusing what you want with implementing a 'toString()' method. However, if you genuinely want to create some way of preserving the contents of your objects, you could also look at Serialization.

Winston
 
georgemc
Posts:16,804
Registered: 5/8/06
Re: converting an object into an String array   
Nov 9, 2009 5:51 AM (reply 4 of 4)  (In reply to original post )

 
Agree with the others above. Turning this meaningful bean back into a lump of raw data - an array - is a step backwards in terms of object orientation. You seem to imply that calling methods is wasteful in some way. Can you qualify that?
 
This topic has 4 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums