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:
Code :
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
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:
Code :
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
}
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:
Code :
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
}
Re: Passing Information between classes
Its missing a definition of the parameter type (eg int)
Code :
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)
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.