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

  • Thread starter Thread starter andreass
  • Start date Start date
  • Tags Tags
    Class Java Line
AI Thread Summary
The discussion revolves around modifying a Java class named "LS" to sum two command line arguments. The original poster seeks assistance in accessing these arguments within the "LS" class. The main class initializes two instances of "LS" using command line arguments. A participant suggests that instead of creating new variables for the string representations of the numbers, the poster should directly access the instance variables of the "LS" objects. Specifically, using `this.sks` for the first instance and `sk.sks` for the second instance allows for the correct retrieval of the string values. This change enables the program to convert the strings to `BigInteger` and compute their sum successfully. The discussion also touches on formatting issues with code presentation, highlighting the importance of using proper tags to maintain readability.
andreass
Messages
14
Reaction score
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
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.
 
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.
 
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top