Barney G
Barney G

Reputation: 109

How to pass an int[] array into main(String []args)?

I'm trying to write a program that begins in the main method, and calls a method that contains an integer array.

Essentially, the program is a basic lift that assumes all inputs are received at the same time. The stops the lift will make are stored in an array called stoppages which is a combination of the stops of the lift going up, and the stops of the lift going down.

I want the program to start in main, and call the method that sorts all of the floors and such. However, when I run the program it changes all of the values of the final array into 0's.

Why is it doing this, and what needs to be done to allow the array values to remain unchanged when the program is run?

This is the main method:

public class user_interface {

public static void main(String[] args) {
    floor_sorting.sorting_floors(args);
     }
}

And this is the method I'm trying to call:

import java.util.*;
 public class floor_sorting{

   public static void sorting_floors(int[] args){

      //bubble sort - sorts out user_up in ascending order
      for (int i = 0; i < user_stops.user_up.length; i++) {
            for (int x = 1; x < user_stops.user_up.length - i; x++) {
                   if (user_stops.user_up[x-1] > user_stops.user_up[x]){
                         int temp = user_stops.user_up[x-1];
                         user_stops.user_up[x-1] = user_stops.user_up[x];
                         user_stops.user_up[x] = temp;   
                   }
            }
      }

      //bubble sort again, but in descending order
      for (int i = 0; i < user_stops.user_down.length; i++) {
            for (int x = 1; x < user_stops.user_down.length - i; x++) {
              if (user_stops.user_down[x-1] < user_stops.user_down[x]){
                     int temp = user_stops.user_down[x-1];
                     user_stops.user_down[x-1] = user_stops.user_down[x];
                     user_stops.user_down[x] = temp;       
              }
      }
      }


      //merges the two separate arrays into one (user_up + user_down = stoppages)  
      int stoppages[] = new int[user_stops.user_up.length+user_stops.user_down.length];
      System.arraycopy(user_stops.user_up, 0, stoppages,0, user_stops.user_up.length);
      System.arraycopy(user_stops.user_down, 0, stoppages, user_stops.user_up.length, user_stops.user_down.length);

      int c = 0;

      do {
            System.out.println("The lift has come to stop at floor " + stoppages[c]);
          System.out.println("The lift has started moving again."); 
          c++;
      } while (c < stoppages.length);

      do {
            System.out.println("This lift has stopped moving forever.");
      } while (c!=stoppages.length);
   }
   }

The above method calls this method:

import java.util.*;
public class user_stops {
//user_up array declaration 
public static int user_up[] = new int [6];{

//Initialising data in array     
user_up[0] = 1;
user_up[1] = 3;

user_up[2] = 2;
user_up[3] = 7;

user_up[4] = 5;
user_up[5] = 8;
}


//user_down array declaration   
public static int user_down[] = new int [6];{

//user_down data initialisation
user_down[0] = 8;
user_down[1] = 5;

user_down[2] = 4;
user_down[3] = 2;

user_down[4] = 6;
user_down[5] = 1;

}

}

Upvotes: 0

Views: 3267

Answers (2)

Achim Schmitz
Achim Schmitz

Reputation: 538

To force your code to run, I simply changed the call in main() :

floor_sorting.sorting_floors(new int[]{1,2,3});

The input array is ignored as I mentioned in my comments. However, it does print out nothing but zeros when it runs. (How did you figure that out?)

The reason is your use of static members in user_stops. These being static means they are not being instantiated at any point. This is not obvious unless you realise that even static members of a class will not always be instantiated.

I added the following statement at the top of sorting_floors():

user_stops user_stops_1 = new user_stops();

The program now runs as expected. (Go ahead... try it.) Another thing that might have worked, though I didn't try it, is to initiate the static arrays in the declaration:

public static  int user_up[] = new int [] {1, 3, 2, 7, 5, 8};
public static int user_down[] = new int [] {8, 5, 4, 2, 6, 1};

However, I think you should re-think your use of the key-word static if you ever want this program to work properly.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718768

I want the program to start in main, and call the method that sorts all of the floors and such. However, when I run the program it changes all of the values of the final array into 0's.

The code you have shown us won't even compile. This line is invalid:

floor_sorting.sorting_floors;

We can't explain what a program does if you don't show us the real code.


If you want to make it compile you need to convert your array of String into an array of int. The simple way is to:

  1. allocate an int[] of the right size (i.e. the same size as the String[] args array),
  2. loop over the range of the arrays, and
  3. use Integer.parseInt(String) to convert the values.

Read the javadocs for the Integer class ...


Finally, you need to pay a lot more attention to style:

  • Your code's indentation is a mess.
  • You are violating the style rules for identifiers. A class name should start with an uppercase letter, and should use "camel case" not underscores.

Code like the above is hard to read, and will get you endless criticism / complaints from co-workers. (And it should lose you marks if this is a marked assignment.)

Upvotes: 5

Related Questions