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

  • Context: Java 
  • Thread starter Thread starter andreass
  • Start date Start date
  • Tags Tags
    Class Java Line
Click For Summary

Discussion Overview

The discussion revolves around modifying a Java program to utilize command line arguments in a specific class ("LS") for summing two numbers. Participants explore how to access and use these arguments effectively within the class structure.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant seeks help on how to access command line arguments in the "LS" class to compute a sum.
  • Another participant suggests that the "add" method does not need separate variables for the numbers, proposing that the instance variables of the "LS" class can be accessed directly using "this" and the passed instance.
  • A later reply confirms that using "this.sks" and "sk.sks" resolves the issue, indicating that the initial problem was related to variable access.
  • There is a discussion about code formatting, specifically the use of tags to preserve indentation when sharing code snippets.

Areas of Agreement / Disagreement

Participants generally agree on the solution to access instance variables for summation, but there is no explicit consensus on the best practices for code formatting in the forum.

Contextual Notes

Participants mention potential issues with code formatting when pasting into the forum, which may affect 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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
2
Views
3K