Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5708
  • Last Modified:

passing a string array

Hi there..
Can you please help me with my question..

how can I pass an array of strings into a constructor while creating an oblect..

in my program..in the test class..i have create this array

string [] names = {"Bill", "John", "Ed", "Brian"};

and now I want to pass it as a parameter to this constructor

College c2 = new College ("ADMC", "London",                 , d1);
and then I will print c2.toString

so how can add pass the array in the space in my constructor
0
swet
Asked:
swet
  • 6
  • 5
  • 3
  • +1
1 Solution
 
grim_toasterCommented:
You should pass it in the same way that you've passed d1.  Simply treat the array of String's as you would any other object, the only difference is that in the constructor for the College class, you will need to specify the parameter as a String[].

Also note that it is String[], not string[]
0
 
tomboshellCommented:
just declare that the constructor will accept a string array.  You can have more than one constructor, if each accepts different parameters (has a different signature.)

public College(String[] data){

}

public College(String name, String location, String other){

}
0
 
swetAuthor Commented:
thanks alot for your help..
but I am still not sure what I have to do..

In the College class I declared the String array as the follow

private String major[] = new String [4];

and in the same Class I added this constructor

public College(String cn, String cLoc, String[] cMj, Director d) {

    cName = cn;
    cLocation = cLoc;
    major = cMj;
    dirc = d;
  }


and in the test class I began th main method with declaring the array in different name

public static void main(String[] args) {
   String cMj1 = {"IT", "SWET", "CNET", "BIT"};


So..do I have to do aloop or anything in order to print the results in the constructor?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TimYatesCommented:
To print out the array you do need a loop, like:

for( int i = 0 ; i < cMj.length ; i++ )
  System.out.println( cMj[ i ] ) ;
0
 
swetAuthor Commented:

if I will declare the loop

String [] cMj1 = {"IT", "SWET", "CNET", "BIT"};

pass it

Director d1 = new Director("Jammie", "POBox 112", 5567783);
    College c2 = new College ("ADMC", "London", cMj1, d1);

I want to print c2 as the follow
System.out.println(c2.toString());

at this situation,,how will I be able to print my array (cMj1) inside the constructor?

thanks alot
0
 
grim_toasterCommented:
If you mean how can you get to print out the array in the System.out.println call, you will need to override the toString() method of Object, and use TimeYates's code to append to a String (or preferably StringBuffer) and return this from the method.

But here's another way to do it, in the College:

public String toString() {
  return "College - cMj = " + Arrays.asList(cmJ);
}
0
 
swetAuthor Commented:

thank alot for your help..

I used the last way..In the college class the toString method looks like this

public String toString() {
    return "College name is: " + cName + "\n College location is: " + cLocation +
        "\n College - cMj = " + Arrays.asList(major)+ "\ncollege director is " + dirc;
  }

but this an error..can't resolve symbole : variable Arrays

than;s in advance
0
 
grim_toasterCommented:
you would need to import java.util.Arrays
0
 
swetAuthor Commented:

Now it works..

thanks very much..and sorry for annoying
0
 
TimYatesCommented:
> Now it works..

Can you accept the comment (or comments) that helped you solve the problem, and award them a grade?  (I would suggest A grade, it makes no difference to the number of points it costs you, but it rewards the expert more) :-)

I would suggest that grim_toasters comment was the one that helped you solve this :-)

If you feel that other people deserve points too, there is a "Split Points" link above the "Post a Comment" box, where you can allocate points to more than one expert (min 20 points per expert)

Tim

> and sorry for annoying

You haven't been annoying :-)  All the experts here are here to help, and now anyone else with the same problem should be able to find this answer and solve it for themselves too in the future :-)  The beauty of Experts Exchange...  By asking this question, you have probably helped someone else later on :-)

Good luck!

Tim.
0
 
TimYatesCommented:
> If you feel that other people deserve points too

I am not saying you should split the points ;-)

(I know for one I don't deserve any)

I was mostly posting this bit of info, so that you know about it next time you need to ask a question :-)

Good luck again!

Tim.
0
 
swetAuthor Commented:

Sorry..but I'm know in here and I don't know anything about points issu?

could you please explaint it to me more?

and thanks Mr.Tim for your words :)
0
 
TimYatesCommented:
Basically, you ask a question for a certain number of points...let's say 100

No matter what, it costs you 100 points for this question.

If you get a good answer or a lot of help, then you can give it an A grade.  This means that the person who answers gets 400 (100 * 4) Expert Points

A "B" grade means that they helped, but didn't really solve your problem, and the expert would get 300 points (100 * 3)

A "C" grade means that they didn't help at all (and you should really consider asking for the question to be removed at http://www.experts-exchange.com/Community_Support)

"Expert Points" can't be used for asking questions, but they are cool, as the more points you have, the higher up the rankings you go (like the Expert "objects" has over 1M Expert Points!!)

Also, if you answer 5000 points of Expert Points each month, you get a free Premium Account for that month (so you have more points for questions) :-)

Basically, if you ever have any problems with the site, or the experts, or the other users, then posting a question on

http://www.experts-exchange.com/Community_Support

Should get you the answer you need (the site Moderators are constantly looking at that area of the site, and answering questions)

Hope this helps!

Welcome to Experts Exchange! :-)

Good luck with your project!

Tim.
0
 
swetAuthor Commented:

now I only have 5 points..and I can't post a new question..

what can I do to increase my points? I really have an urgent question..

thanks alot for your explanation :)
0
 
TimYatesCommented:
You have 3 options...

1) Wait a few days, till the number of points you have increases (you get a few points a day...not sure how many)
2) Purchase a Premium account from https://secure.experts-exchange.com/Programming/Programming_Languages/Java/editPremiumServices.jsp
3) Answer 5000 points worth of questions, and wait till the 1st December :-(

I don't think there is any other way :-(

Tim
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now