Question:
JAVA - how to use command line arguments in more than 1 class?
nelogjisks
2010-10-28 07:59:40 UTC
Hello.
I have this JAVA code, I have to change only class "LS", so that program gets sum and displays it. But I don't know how to get command line arguments to LS class (I need them in "??", to get sum). Can anyone help? :)

public class S
{
public static void main(String[] args)
{
LS ls1 = new LS(args[0]);
LS ls2 = new LS(args[1]);
ls1.add(ls2);
ls1.display();
}
}

class LS
{
private String sks;
LS(String str)
{
sks = str;
}
public void add(LS sk)
{
String s1 = "??";
String s2 = "??";
BigInteger p = new BigInteger(s1);
String sum = p.add(new BigInteger(s2)).toString();
sks=sum;
}
public void display()
{
System.out.println(sks);
}
}
Four answers:
modulo_function
2010-10-28 08:11:34 UTC
Command line arguments are only obtained by main(String[] args). They are stored that that string array, args. I you need to pass them to other classes then you have choices:



1) make them class variables and public



2) code a public method that can access them (preferred).



Looking at your code there's an easier way for you but I don't you just want:



String s1 = this.sks;

String s2 = sk.sks;





Btw, that's a terrible choice of identifiers!
Frecklefoot
2010-10-28 08:22:50 UTC
First off, it's Java, not JAVA.



Second, I see A LOT of problems with your code. What is your application trying to do? Add two numbers from the command-line? There are a lot more straight-forward ways to do this. I would just put a method in S that does the calculation, but you can do it with a separate class as well.



The first change you want to make it to give your variables logical names. It's difficult to follow s1, s2 and LS. Give your variables names that are meaningful. It will help you when you start writing more complex applications.



The command-line arguments are in an array and can be passed straight to LS in the same way:



public void add( String[] numbers )



or you can just do what you're already doing: passing them as Strings. But you want them as numbers, right? Do this:



int myNumber = Integer.parseInt( stringThatHasAnInt );



Make the only method for LS a static method called add:



public static int add( int num1, int num2 )

{

// you fill this in...

}



And call it like this:



int mySum = LS.add( Integer.parseInt( args[0], args[1] );



That should be enough to get you in the right direction. HTH
AnalProgrammer
2010-10-28 08:18:37 UTC
You are accessing the parameters already when you call the LS class constructor.

LS ls1 = new LS(args[0]);

LS ls2 = new LS(args[1]);



Now all you have to so is assign the right class variable to the strings, as has already been answered.



Have fun.
2016-11-07 04:11:33 UTC
while you're launching from an IDE, like JCreator, Netbeans, Eclipse... those are actually not the CommandLine invocation of a Java application. in the previous days we made a folder, used Notepad and typed the code such as you teach. Then... >$ cd /myPrograms/Java /myPrograms/Java >$ javac CLCDemo.java /myPrograms/Java >$ java CLCDemo.java one-potato 2-potato 3-potato greater and it might output the 4 args edit:::::::::::: I in simple terms now copied your keystrokes, made a document CLDemo.java person@person-laptop: ~$ cd '/abode/person/laptop/testCmmdline' person@person-laptop: ~/laptop/testCmmdline$ javac CLDemo.java person@person-laptop: ~/laptop/testCmmdline$ java CLDemo one 2 3 There are 3 command-line arguments. they are: arg[0]: one arg[a million]: 2 arg[2]: 3 person@person-laptop:~/laptop/testCmmdlin...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...