Link to home
Start Free TrialLog in
Avatar of swet
swet

asked on

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
Avatar of grim_toaster
grim_toaster

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[]
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){

}
Avatar of swet

ASKER

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?
Avatar of TimYates
To print out the array you do need a loop, like:

for( int i = 0 ; i < cMj.length ; i++ )
  System.out.println( cMj[ i ] ) ;
Avatar of swet

ASKER


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
ASKER CERTIFIED SOLUTION
Avatar of grim_toaster
grim_toaster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of swet

ASKER


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
you would need to import java.util.Arrays
Avatar of swet

ASKER


Now it works..

thanks very much..and sorry for annoying
> 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.
> 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.
Avatar of swet

ASKER


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 :)
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 https://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

https://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.
Avatar of swet

ASKER


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 :)
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://www.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