JAVA - how to use command line arguments in more than 1 class

In summary: 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? :) In summary, the code needs to change from:public class S {to:public class S {and the command line arguments need to be passed to the LS class as "???".
  • #1
andreass
16
0
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);
}
}
 
Technology news on Phys.org
  • #2
I added [ code] and [ /code] tags (without the extra spaces) around your code to preserve your formatting.
andreass said:
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? :)
Code:
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);
    }
}

I don't think you need the s1 and s2 variables in your add method. The call to add() in main looks like this:
Code:
      ls1.add(ls2);
This call uses the ls1 instance (whose string member contains the string representing the first number) and passes ls2 (whose string member contains the string representing the second number).

What I think you are missing is not using this to be able to access ls1's sks variable.

For example, this->sks gets you access to sks in ls1 and ls2.sks gets you access to sks in ls2. Of course, you'll need to convert these strings to BigInteger instances before adding them, but you seem to know how to do that.

I haven't done any Java for about 15 years, so I might be slightly off in a detail, but I think my basic thrust is correct.
 
  • #3
Thank you for the answer.
I needed
Code:
String s1 = this.sks;
String s2 = sk.sks;
Now everything works. :)
btw. sorry for terrible identification in code.
 
  • #4
andreass said:
Thank you for the answer.
I needed
Code:
String s1 = this.sks;
String s2 = sk.sks;
Now everything works. :)
btw. sorry for terrible identification in code.
The identification was OK, but the indentation was not so hot.:wink:

Actually, your code was indented nicely, but if you copy and paste it into the input window, you lose all that nice formatting. Putting [ code] and [ \code] around it preserves the indentation.
 
  • #5


Hello,

To use command line arguments in more than one class, you can pass the arguments as parameters to the constructor of the class that needs them. In this case, you can pass the arguments to the constructor of the LS class and then use them in the add() method.

For example:

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();
sk.sks=sum;
}
public void display()
{
System.out.println(sks);
}
}

In the main method, you can pass the arguments as parameters to the constructor of the LS class. Then, in the add() method, you can use these parameters to perform the desired operation and update the sks variable. Finally, you can use the display() method to print the updated value of sks.

Hope this helps. Let me know if you have any further questions.
 

1. How do I pass command line arguments to a Java program?

Command line arguments can be passed to a Java program by specifying them after the java command when running the program. For example, if your program is called "MyProgram" and you want to pass two arguments "arg1" and "arg2", you would use the command java MyProgram arg1 arg2. These arguments can then be accessed within your program using the args parameter in the main method.

2. Can I use command line arguments in more than one class in my Java program?

Yes, command line arguments can be accessed in any class within your Java program, as long as the arguments are passed to the main method of the class. This can be achieved by calling the main method of the class from the main method of your program, passing the args parameter as an argument, or by using static variables to store the arguments for access in other classes.

3. How can I handle different types of command line arguments in my Java program?

You can handle different types of command line arguments in your Java program by using the args parameter and converting the arguments to the desired data type using methods such as Integer.parseInt() or Double.parseDouble(). You can also use the instanceof keyword to check the data type of the argument before converting it.

4. Is it possible to pass multiple arguments of the same data type through the command line?

Yes, you can pass multiple arguments of the same data type through the command line by separating them with spaces. These arguments can then be accessed as elements of the args array in your program.

5. What happens if I don't provide any command line arguments when running my Java program?

If no command line arguments are provided when running a Java program, the args parameter in the main method will be an empty array with a length of 0. Therefore, if your program requires command line arguments, you should include a check for the length of the args array to ensure that the correct number of arguments have been provided.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
633
  • Programming and Computer Science
Replies
3
Views
772
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
1
Views
750
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top