How to pass (String[] args), as a parameter.

/**

* StringMethods

*

* @author (Robin)

* @version (1)

*/

publicclass StringMethods

{

private String[] args;

privateint numArgs;

publicvoid printIt(String[] args)

{

System.out.print ("You have entered " + numArgs +" Strings");

if (numArgs > 10)

{

int temp = numArgs;

System.out.print (", the last " + (numArgs - 10) +" will not be processed.");

numArgs = temp;

}

for (int i = 0; (i < numArgs) && (i < 10); i++)

System.out.println ("[" + i +"] : " + args[i] +" is " + (args[i]).length() +" characters long.");

}

publicvoid getArgs(String[] input)

{

numArgs = input.length;

arraycopy(input, 0, args, 0, numArgs-1);

}

}

arraycopy(input, 0, args, 0, numArgs-1)

This is my method class for my main method StringArray.

What I want to know is how to pass command line arguments, i.e. {"example1", "example2", "example3"} from my main method to my method class?

[1942 byte] By [spiderymessa] at [2007-10-2 13:06:08]
# 1
The main method receives command line arguments in the form of an array of Strings. You can pass this the same way you pass anything else. Make a method that accepts an array of Strings for a parameter and pass the array.
kablaira at 2007-7-13 10:30:20 > top of Java-index,Java Essentials,Java Programming...
# 2

You have a few problems:

1. Don't name a member variable the same as a parameter; it's a little confusing.

2. In you're method ("getArgs") you are referencing "args", I think you mean to write "input".

3. There really is no need for that member variable anyway (they one called "args").

4. To pass from your main method you're going to need to create a new instance of your class like so:public static void main(String[] args){

StringMethods m = new StringMethods();

m.printIt( args );

m.getArgs( args );

}

Okay?

Also, maybe consider having a constructor that takes "String[] args" instead of each method taking it. The constructor can then assign it to a member variable and you're other methods can just reference that.

kablaira at 2007-7-13 10:30:20 > top of Java-index,Java Essentials,Java Programming...
# 3

/**

* StringMethods

*

* @author (Robin)

* @version (1)

*/

public class ArgMethods

{

private String[] args = new String[0];

private int numArgs;

public String[] input = {"null", "null"};

// constructor that takes string args to be manipulated by methods

public ArgMethods(String[] input)

{

numArgs = input.length;

String[] args = new String[numArgs];

System.arraycopy(input, 0, args, 0, input.length);

}

public void printIt()

{

if (args.length > 0)

{

System.out.print ("You have entered " + numArgs + " Strings");

if (numArgs > 10)

{

int temp = numArgs;

System.out.print (", the last " + (numArgs - 10) + " will not be processed.");

numArgs = temp;

}

for (int i = 0; (i < numArgs) && (i < 10); i++)

System.out.println ("[" + i + "] : " + args + " is " + (args).length() + " characters long.");

}

else

System.out.println ("You must enter at least one String at the command line.");

}

public void reverseIt()

{

if(args.length > 0)

{

int next = args.length-1;

for (int index = 0; index < args.length/2; index++)

{

String temp = args[index];

args[index] = args[next];

args[next] = temp;

next--;

}

}

}

}

/**

* StringArray

*

* @author (Robin)

* @version (1)

*/

public class stringArray

{

public static void main (String[] args)

{

int numArgs = args.length;

// correct for output if strings less than 10 and greater than 0

if (numArgs > 0)

{

System.out.print ("You have entered " + numArgs + " Strings");

if (numArgs > 10)

{

int temp = numArgs;

System.out.print (", the last " + (numArgs - 10) + " will not be processed.");

numArgs = temp;

}

for (int i = 0; (i < numArgs) && (i < 10); i++)

System.out.println ("[" + i + "] : " + args + " is " + (args).length() + " characters long.");

}

else

System.out.println ("You must enter at least one String at the command line.");

ArgMethods test = new ArgMethods( args );

// test.printIt();

// test.reverseIt();

// test.printIt();

}

}

*almost finished -- posting this up here so i can get it at home ((only accessible e-mail down plus can't find pen drive))..

spiderymessa at 2007-7-13 10:30:20 > top of Java-index,Java Essentials,Java Programming...
# 4

/**

* StringArray

*

* @author (Robin)

* @version (1)

*/

public class stringArray

{

public static void main (String[] args)

{

int numArgs = args.length;

// correct for output if strings less than 10 and greater than 0

if (numArgs > 0)

{

System.out.print ("You have entered " + numArgs + " Strings");

if (numArgs > 10)

{

int temp = numArgs;

System.out.print (", the last " + (numArgs - 10) + " will not be processed.");

numArgs = temp;

}

for (int i = 0; (i < numArgs) && (i < 10); i++)

System.out.println ("[" + i + "] : " + args[i] + " is " + (args[i]).length() + " characters long.");

}

else

System.out.println ("You must enter at least one String at the command line.");

ArgMethods test = new ArgMethods( args );

// test.printIt();

// test.reverseIt();

// test.printIt();

}

}

/**

* StringMethods

*

* @author (Robin)

* @version (1)

*/

public class ArgMethods

{

private String[] args = new String[0];

private int numArgs;

public String[] input = {"null", "null"};

// constructor that takes string args to be manipulated by methods

public ArgMethods(String[] input)

{

numArgs = input.length;

String[] args = new String[numArgs];

System.arraycopy(input, 0, args, 0, input.length);

}

public void printIt()

{

if (args.length > 0)

{

System.out.print ("You have entered " + numArgs + " Strings");

if (numArgs > 10)

{

int temp = numArgs;

System.out.print (", the last " + (numArgs - 10) + " will not be processed.");

numArgs = temp;

}

for (int i = 0; (i < numArgs) && (i < 10); i++)

System.out.println ("[" + i + "] : " + args[i] + " is " + (args[i]).length() + " characters long.");

}

else

System.out.println ("You must enter at least one String at the command line.");

}

public void reverseIt()

{

if(args.length > 0)

{

int next = args.length-1;

for (int index = 0; index < args.length/2; index++)

{

String temp = args[index];

args[index] = args[next];

args[next] = temp;

next--;

}

}

}

}

forgot to put the code brackets

spiderymessa at 2007-7-13 10:30:20 > top of Java-index,Java Essentials,Java Programming...