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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top