User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,442 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,220 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 3136 | Replies: 2
Reply
Join Date: May 2005
Posts: 3
Reputation: markymarkb1000 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
markymarkb1000 markymarkb1000 is offline Offline
Newbie Poster

sorting through an array of objects

  #1  
May 7th, 2005
Hi...I have created a private static main method which instantiates an array of 3 different types of objects depending on the choices entered in on the command line. Here is my code:

public static void main(String[] args)
{
//creates local variable currentIndex

//creates employee array of type worker which can hold up to 100 emloyee objects
Worker[] employee = new Worker[100];
//do/while loop
int i = 0;
do{
//if "w" for "worker" is entered, call Worker Class constructor, store
//pointer to worker name in employee array
if (args[i].equalsIgnoreCase("w"))
{
//store worker name in employee array
employee[currentIndex] = new Worker(args[i+1]);
//increment currentIndex counter by 1
currentIndex++;
//increment counter i by 2
i+=2;
}

//if "p" for "producer" is entered, call Producer Class constructor, store
//pointer to producer name and prod. target in employee array
else if (args[i].equalsIgnoreCase("p"))
{
//store producer name in next avail array slot, morph string value for
//production target and store as integer in next array slot
employee[currentIndex] = new Producer(args[i+1], Integer.parseInt(args[i+2]));
//increment currentIndex counter by 1
currentIndex++;
//increment counter i by 2
i+=3;

}
//if "m" for "manager" is entered, call Manager Class constructor, store
//pointer to manager name, number of subordinates, and whether or not this
//manager is liked in next array slot
else if (args[i].equalsIgnoreCase("m")){
//store manager name in next avail array slot, morph string value for
//number of subordinates and store as integer in next array slot along with
//liked/disliked result in following slot
employee[currentIndex] = new Manager(args[i+1], Integer.
parseInt(args[i+2]), args[i+3]);
//increment currentIndex counter by 1
currentIndex++;
//increment counter i by 2
i+=4;
}
else
{
//if choice was entered other than m,p,w, print error message
System.out.println("Must be M, P, or W");
//exit program
System.exit(1);
}
//end while loop
}while(i <args.length);
}
}//end class

I am supposed to create a private static method which is called by this main class once the array has been instantiated and called if the "Producer" class has an integer value greater than 50 (this is the parseInt(args[i+2]) in the Producer constructor call).

I can't quite get it to work.

Am trying to use this code:

for (int j =0; j < employee.length; j++){
if (employee[j].equals("Producer"))
{
System.out.println(Producer(args[j+1], (args[j +1], Integer.parseIntargs[j+2]));

}
}

I keep getting an error - cannot resolve method Producer(java.lang.string, int)

any help?

thanks.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: sorting through an array of objects

  #2  
May 7th, 2005
If producer is static, then you shouldn't be calling it using the new keyword. It should be, ClassName.methodName().

I would have thought that would cause a "non static method being called from a static context" error though. Since that wasn't your error, then it could be that your method signature isn't correct, or that the parameter list isn't correct.
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: sorting through an array of objects

  #3  
May 7th, 2005
By the way, use code tags so your code is easier to look at.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 2:50 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC