•
•
•
•
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
![]() |
•
•
Join Date: May 2005
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Dynamic memory for an array of objects (C++)
- Sorting 2D Dynamic array of integers , Snake Style (C)
- single array objects (Java)
- Help : Pointers to array of class objects . (C++)
Other Threads in the Java Forum
- Previous Thread: Multiple Classes
- Next Thread: Importance of checking the current thread?



Linear Mode