Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By DiamondSoul

Thread: Access command-line arguments from another class

  1. #1
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Access command-line arguments from another class

    I want to create a program that accepts 9 positive numbers. For the sake of brevity, let me just say the first number represents the number of rectangles to be created, second and third, x, y coordinates, 4th and 5th rectangle height and width and so on. I also want to create another class named Rectangle that has four member variables, namely the x and y coordinates from the program (to be manipulated) and the height and width which remain unchanged.

    My problem is that these arguments are to be passed as command-line arguments to the program, meaning they only exist within the main method, and are inaccessible outside of it. How can I access them outside the main method, and use them in another class, e.g. in the Rectangle class?

    Here is my code, and where I am stuck:

    Java Code:
    public class Example{	
    	public static void main(String[] args) {
    		int x = 0, y = 0, number= 0, height = 0, width= 0, xk = 0, yk = 0, sx = 0, sy = 0;
    		try {
    	        // Parse the string argument into an integer value.
    	        x = Integer.parseInt(args[0]); // 
    	        y = Integer.parseInt(args[1]); // and so on till //arg[8]	        	        	        
    	    }
    	    catch (NumberFormatException nfe) {	        
    	        System.out.println("Must be a number!");
    	        System.exit(1);
    	    }	    
    	    
    	}
    	    
    }
    
    //Here is the class that needs accessing those values
    class Rectangle{
            int x = 0, y = 0, height= 0, width= 0;
    //do something with x and y but first need to be able to access them
    	Rectangle(int x, int y, int b, int h) {};
    //number of to-be-created rectangles determined from the num inputed in //command-line but can't access the number variable	
    	Rectangle[] rectangle = new Rectangle[number];		
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Access command-line arguments from another class

    One way:

    Java Code:
    public class Example{   
        public static void main(String[] args) {
            int x = 0, y = 0, number= 0, height = 0, width= 0, xk = 0, yk = 0, sx = 0, sy = 0;
            try {
                // parse first number to get number of rectangles, n
                // create array of n Rectangles
                // use a for loop that loops n times to get remaining data from chart and 
                // with that data initialize each Rectangle in the array
            }
            catch (NumberFormatException nfe) {         
                System.out.println("Must be a number!");
                System.exit(1);
            }       
             
        }
             
    }

  3. #3
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Access command-line arguments from another class

    Java Code:
    public class Example{  
        public static void main(String[] args) {
            int x = 0, y = 0, number= 0, height = 0, width= 0, xk = 0, yk = 0, sx = 0, sy = 0;
            try {
               number = Integer.parseInt(args[0]);
    	        Rectangle[] rectangle= new Rectangle[number];
    	        for(int i = 0; i < number; i++){	        	
    	        rectangle[i].x = x; recntable[i].y = y; 
    	        rectangle[i].width= width; rectangle[i].height= height;}
                // However, beyond this I am stuck. Besides, this does not solve my problem of
    //being able to access this values in main in a separate class (see bottom) 
            }
            catch (NumberFormatException nfe) {        
                System.out.println("Must be a number!");
                System.exit(1);
            }      
              
        }
              
    }
    
    class Rectangle {
    int x, y, width, height;
    //I want to be able to access the values of x, y, width and height from the //main method and then manipulate them inside this class
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Access command-line arguments from another class

    You are perfectly at liberty to pass args about like any other variable. In particular things you construct in main() can be sent args as a parameter. Or an array of int values you construct from the command line if that is more useful.

    But it's not really clear what you are trying to do here. Why does your Rectangle class include an array as one of its properties?

  5. #5
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Access command-line arguments from another class

    But args are arguments in the main method. So in essence they are like local variables of the main method, and I can't access them outside of main, or am I getting things wrong? I mean I have tried accessing these values outside of the main method but I can't.

    Java Code:
    public class Example{  
        public static void main(String[] args) {
    //These arguments are command-line arguments and are read during the execution of the program, or am I wrong?
            int x = 0, y = 0, number= 0, height = 0, width= 0, xk = 0, yk = 0, sx = 0, sy = 0;
            try {
                int number = args[0];
                int x = args[i]; // and so on
            }
            catch (NumberFormatException nfe) {        
                System.out.println("Must be a number!");
                System.exit(1);
            }      
            //Now the args die here, and are inaccessible outside of this function, right?  
        }
              
    }
    
    class Rectangle{
    
    //But I need those values here so that I can create a separate class here that manipulates the value of x and y for each rectangle created 
    //according to a certain formula. I also need an array because I need to create a certain number of rectangles (number) that can only be known 
    //during execution of the program as part of command-line arguments.
    //So I thought it is best to do this:
    Rectangle [] rectangle = new Rectangle[number];
    //if there is a simpler (and better) way of doing it, I will appreciate it.
    //But I can't access those command-lien arguments read during execution of the program outside of the main method, even in the Example class, let alone //accessing them here.
    //What I am trying to do is read in some  integer values during execution of the programs, then manipulate some of these integers in another class
    }

  6. #6
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Access command-line arguments from another class

    What you have to do is PASS the parameters in question from Main to the other class (probably in a constructor). As a simpler example, suppose you just had a single String parameter that is in args[0] in Main that you want to do something with in another class. That would be done like this:

    Java Code:
    public class Main{
        public static void main(String[] args){
            new OtherClass(args[0]).doStuff();
        }
    }
    
    class OtherClass{
        private String value;
        public OtherClass(String value){
            this.value=value;
        }
        public void doStuff(){
            System.out.println(value);
        }
    }
    If you run the above code, it will output the first command line argument to the console.
    pbrockway2 likes this.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Access command-line arguments from another class

    Java Code:
    int x = args[i]; // and so on
    This code won't compile because you can't assign a String instance to an int variable.

    I need those values here so that I can create a separate class here that manipulates the value of x and y for each rectangle created
    according to a certain formula.
    So you pass either args or its elements to the instances of this separate class along the lines shown by DiamondSoul. It's a general point: if you "need values here" where "here" is some class or other, you pass those values to either a constructor or a method of that class.

    I also need an array because I need to create a certain number of rectangles (number) that can only be known
    during execution of the program as part of command-line arguments.
    Notice that creating rectangles is no part of being a rectangle. (Unless geometry has become more interesting since I went to school.) So the need to create rectangles doesn't, itself, necessitate or even suggest any particular way of implementing Rectangle. In particular there is no obvious need for a Rectangle[] as part of the definition of Rectangle. It is within the main() method - or in some code called by that method - that the rectangles will be created, but within the Rectangle class that each rectangle's particular properties will be set.

    Fubarable gave an illustration of this: rectangles created within the main() method and passed information from the command line so they could configure themselves.

    -----

    It might be that you are the victim of the tyranny of "need". You have some intended result and a particular way of going about it is imposing itself as being "needed". Better, perhaps, to step back from the problem and focus on describing that intended result. Maybe someone can suggest how that result could be implemented.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Access command-line arguments from another class

    Quote Originally Posted by pbrockway2 View Post
    Unless geometry has become more interesting since I went to school.
    Waaaay back when they still used slide rules? ;)

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default offtopic

    Quote Originally Posted by Junky View Post
    Waaaay back when they still used slide rules? ;)
    Sort of on a cusp: the earliest calculators were beginning to be used, but we did have to learn how to use sliderules and log tables. In my last year of highschool a local hospital donated a second generation computer they were taking out of service. Since no-one knew how to drive it, I was given the keys to its room (it needed a room) and the manual.

Similar Threads

  1. command line arguments
    By shruthichandru in forum New To Java
    Replies: 11
    Last Post: 11-20-2010, 02:02 AM
  2. command line arguments
    By evermore in forum New To Java
    Replies: 5
    Last Post: 03-28-2010, 05:08 AM
  3. command line arguments
    By jttslg in forum Advanced Java
    Replies: 23
    Last Post: 03-24-2010, 05:06 PM
  4. Command-Line Arguments
    By mustachMan in forum New To Java
    Replies: 5
    Last Post: 02-26-2010, 03:13 AM
  5. arguments in command-line
    By girlet18 in forum New To Java
    Replies: 2
    Last Post: 01-21-2010, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •