Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Passing Information between classes

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Passing Information between classes

    Hello everyone,

    I'm trying to pass an argument i receive from the command line in a main class to another class where i've created an array. The argument will designate how big the array should be. If you can't already tell I'm brand new to Java and I'm experiencing some frustration in trying to overcome what should be a simple matter.

    I stored the argument in a variable MAX in the main class, and in the second class i used the variable MAX again, thinking that the second class would recognize it from the first but obviously this is incorrect. Any help would be greatly appreciated and here's the code. I have first shown the main class, then the second class. Thanks in advance:

    import java.io.*;
     
    public class MidTwo {
     
      public static void main (String[] args) throws IOException {
        int MAX;
    	 int i;
    	 BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
     
        MAX = Integer.parseInt(args[0]);
    	 if (MAX<=0) {
    	   System.out.println("Command line needs to be a positive integer");
    		return; 
    		}
     
    	 MyArray gment; gment = new MyArray(MAX);
     
    	}
    }
     
     
     
    import java.io.*;
     
    public class MyArray {
      double num;
      String line;
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
     
     
      MyArray (int MAX) throws IOException {
        String line;
     
       double array[] = new double[MAX];
     
         for (int i=0; i<MAX; i=i++) {
          System.out.println("what number?");
    	   line = in.readLine();
          num = Double.parseDouble(line);
    			array[i] = num;
    			{ break;
    			 }
    }
    }
    }

    Sam


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Passing Information between classes

    What exactly is the error you are experiencing? The way in which you are passing MAX seems fine. Perhaps the break statement in your MyArray constructor is preventing the filling in of the array? Or the i variable in the loop, which can give you an infinite loop:
    for ( int i = 0; i < MAX; i++ ){//ok, and standard
     
    }
     
    for ( int i = 0; i < MAX; i = ++i ){//ok
     
    }
     
    for ( int i = 0; i < MAX; i = i++ ){//this results in an infinte loop
     
    }
    Last edited by copeg; December 8th, 2009 at 03:39 PM.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Passing Information between classes

    First of all, thank you for taking the time to reply.

    Actually, I removed the break statement and there is no difference.

    I tried declaring the variable MAX as a public static int, because I was told that in this way I could access it in any class by calling it as MidTwo.MAX

    However, now I receieve an identifier expected error at line 6 of the MyArray class. The arrow indicating where the error is points to the closing parentheses of MyArray (MidTwo.MAX).
    Here is my code, thank you for your help:

     
    import java.io.*;
     
    public class MidTwo {
     
        public static int MAX;
     
        public static void main (String[] args) throws IOException {
        int i;
    	 BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
     
        MAX = Integer.parseInt(args[0]);
    	 if (MAX<=0) {
    	   System.out.println("Command line needs to be a positive integer");
    		return; 
    		}
     
    	 MyArray gment; gment = new MyArray(MAX);
     
    	}
     
     
    }
     
    import java.io.*;
     
    public class MyArray {
      double num;
      String line;
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
     
     
      MyArray (MidTwo.MAX) throws IOException {
        String line;
     
       double array[] = new double[MidTwo.MAX];
     
         for (int i=0; i<MidTwo.MAX; i=i++) {
          System.out.println("what number?");
    	   line = in.readLine();
          num = Double.parseDouble(line);
    			array[i] = num;
    	   }//end of for loop
     
       }//end of constructor
     
    }
    Last edited by SKhoujinian91; December 8th, 2009 at 03:34 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Passing Information between classes

    Its missing a definition of the parameter type (eg int)
    MyArray (int MAX) throws IOException {
    //etc...
    }


    But with a static, there isn't a need to pass the info as a parameter, just access it via MidTwo.MAX. I may have edited my post while you were writing yours, be sure that the loop isn't infinite (see my post above)

  5. The Following User Says Thank You to copeg For This Useful Post:

    JavaPF (December 8th, 2009)

  6. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Passing Information between classes

    Thank you so much, your last post did the trick!
    I didn't think it was possibly to simply pass variables by name from one class to another.

Similar Threads

  1. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM
  2. Having trouble printing object information in main class
    By KingLane in forum Object Oriented Programming
    Replies: 1
    Last Post: October 11th, 2009, 06:53 PM
  3. [SOLVED] How to link two different class?
    By John in forum Object Oriented Programming
    Replies: 11
    Last Post: April 27th, 2009, 02:57 PM
  4. Why output is displaying 0 for calculation in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2009, 01:18 PM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM