Solved

passing a string array

Posted on 2003-11-13
15
5,696 Views
Last Modified: 2010-03-31
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
Comment
Question by:swet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9745816
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
 
LVL 7

Expert Comment

by:tomboshell
ID: 9745825
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
 

Author Comment

by:swet
ID: 9745990
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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 35

Expert Comment

by:TimYates
ID: 9746006
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
 

Author Comment

by:swet
ID: 9746026

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
 
LVL 7

Accepted Solution

by:
grim_toaster earned 125 total points
ID: 9746108
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
 

Author Comment

by:swet
ID: 9746173

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
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9746188
you would need to import java.util.Arrays
0
 

Author Comment

by:swet
ID: 9746205

Now it works..

thanks very much..and sorry for annoying
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9746310
> 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
 
LVL 35

Expert Comment

by:TimYates
ID: 9746327
> 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
 

Author Comment

by:swet
ID: 9746967

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
 
LVL 35

Expert Comment

by:TimYates
ID: 9746990
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
 

Author Comment

by:swet
ID: 9747003

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
 
LVL 35

Expert Comment

by:TimYates
ID: 9747036
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question