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