0

My question is quite simple I would like to create an array and populate it with some string items. Below is a simple code containing two classes and one main method (pretty basic program).

public class CompanyDetails {
public static void main (String[] args){

    CompanyInput apple = new CoInput();
    apple.coDetailsSummary("Apple","USA", "NA");
    apple.computeNoOfJoinees();

    CompanyInput htc = new CoInput();
    htc.coDetailsSummary("HTC", "Thailand", "NA");
    htc.computeNoOfJoinees();

}

}

public class CompanyInput {
float noOfJoinees;
String companyName;
String address;
String phoneNumber;
int randomInt,randomInt1;
int noOfEmployees;
Array gh;

void coDetailsSummary(String companyName, String address, String phoneNumber) {
    // TODO Auto-generated method stub

    System.out.println("Company Name                              : "+companyName); 
    System.out.println("Address                                   : "+address); 
    System.out.println("Contact Information                       : "+phoneNumber); 

}
void computeNoOfJoinees(){
    float m1,m2, m3;
    Random randomGenerator = new Random();
    for (int idx = 1; idx <= 10; ++idx)
        randomInt = randomGenerator.nextInt(30);
        randomInt1 = randomGenerator.nextInt(20);

        m1 = randomInt;
        m2 = randomInt1;
    m3 = m1+m2/100;
    System.out.print("The number of joinees joining every month : "+m3);
    System.out.println("\n");
}

}

I would like to create an array with name as the "companyName" and populate it with all the values that will be given as output.

For e.g

Company Name                              : Apple
Address                                   : USA
Contact Information                       : NA
The number of joinees joining every month : 13.0

All of these values must be inserted into an array automatically when I run the program. The result should look like this

Apple[] = {Apple,USA,NA,13.0}

how can I achieve this ?

Vinoth
  • 1,339
  • 6
  • 23
  • 42
  • possible duplicate: http://stackoverflow.com/q/5061721/876298 or this: http://stackoverflow.com/questions/6458463/java-dynamically-fill-array-not-vector-arraylist – Alex K Nov 18 '11 at 06:19
  • no Alex K, it is not a duplicate. – Vinoth Nov 18 '11 at 06:25

5 Answers5

1

Change your

float noOfJoinees;
String companyName;
String address;
String phoneNumber;
int randomInt,randomInt1;
int noOfEmployees;
Array gh;

void coDetailsSummary(String companyName, String address, String phoneNumber) {
    // TODO Auto-generated method stub

    System.out.println("Company Name                              : "+companyName); 
    System.out.println("Address                                   : "+address); 
    System.out.println("Contact Information                       : "+phoneNumber); 

}

into

void coDetailsSummary(String argCompanyName, String argAddress, String argPhoneNumber) {
    // TODO Auto-generated method stub
this.companyName = argCompanyName

// similarly for the other arguments.

}

And then add it to a ArrayList and loop through them and print it.

r0ast3d
  • 2,639
  • 1
  • 14
  • 18
1

Use an ArrayList, for dynamically adding elements In your case, it would be an ArrayList of CompanyInput objects.

ArrayList<CompanyInput>companyName = new ArrayList<CompanyInput>();
companyName.add(new CompanyInput("Apple","USA","NA",13.0));
companyName.add(new CompanyInput("MSoft","USA","NA",15.0));

etc...

Hope that helps !

Saurabh
  • 771
  • 1
  • 7
  • 18
1

In the main method, create an ArrayList of type CompanyInput as follows: List listOfCompanies = new ArrayList();

Now, one by one add all the instances of CompanyInput to this list. For e.g. To add "apple" to the list: listOfCompanies.add(apple);

Also Override the toString method of Object class into the CompanyInput class. To know how to overirde toString you can refer : http://www.javabeat.net/tips/12-overriding-the-tostring-method-in-object-cl.html

Now, just print the list just like printing any variable:- System.out.println(listOfCompanies);

And the output of the toString method overridden will be printed to the console for all the instances added to the list.

whitehat
  • 2,381
  • 8
  • 34
  • 47
  • I tried your idea. I am getting an output like this [CompanyInput@190d11, CompanyInput@a90653, CompanyInput@de6ced, CompanyInput@c17164, CompanyInput@1fb8ee3, CompanyInput@61de33]. It's of object type. How will convert this to display the string values ? – Vinoth Nov 18 '11 at 07:06
  • @Vinoth : The output is such because you must not have overridden the toString method in the class whose object's list is made in the ArrayList. Override the toString method and return some string output. Then this will be the output instead of CompanyInput@de6ced.. – whitehat Dec 02 '11 at 06:09
0

If your java version supports varargs then you can do:

private static String[] createDynamicStringArray(String... items){
  return items;
}

usage:

String[] items = createDynamicStringArray("item1", "item2", "item3");

But I would suggest to rethink your design.

Alex Nikolaenkov
  • 2,505
  • 20
  • 27
0

You should use List instead. Arrays doesnot support this feature.

Please google Advantages of List over arrays in Java

Selvakumar Ponnusamy
  • 5,363
  • 7
  • 40
  • 78