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

  • Java
  • Thread starter andreass
  • Start date
  • #1
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);
}
}
 

Answers and Replies

  • #2
33,960
5,615
I added [ code] and [ /code] tags (without the extra spaces) around your code to preserve your formatting.
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
16
0
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
33,960
5,615
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.
 

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

  • Last Post
Replies
1
Views
3K
Replies
4
Views
2K
Replies
3
Views
2K
  • Last Post
2
Replies
35
Views
4K
Replies
3
Views
2K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
3
Views
1K
Top