Katy
Katy

Reputation: 13

JAVA: Passing an array to a method

I can't seem to pass my array to a secondary method. It only sees one of the 10 inputs, so I can't properly use it in the secondary method.

public static void main(String[] args) {
    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.println("Enter some numbers: ");
    double [] numbers = new double [10]; 
    int n = 0; 
    int i = 0;
    while(n < numbers.length) { 
        numbers[i] = input.nextDouble(); 
        n+=1; 
    }
    System.out.println(min(numbers));
} 

public static double min(double[] array) { 
    System.out.println(array[1]);
    double smallest = array[0];
    for (int l = 1; l < array.length; l++) { 
        if (array[l] < smallest) { 
            smallest = array[l]; 
            System.out.println("Your smallest = " + smallest);
        }

    }
    return 0; 
}

Upvotes: 1

Views: 79

Answers (2)

user4426213
user4426213

Reputation:

while (n < numbers.length) { 
    numbers[i] = input.nextDouble(); 
    n+=1; 
}

variable i is never being changed so you are assigning each new number to the same spot in the array overwriting the previous number.

Just use your n variable instead:

while (n < numbers.length) { 
    numbers[n] = input.nextDouble(); 
    n += 1; 
}

Upvotes: 1

victor175
victor175

Reputation: 614

In the first while loop, the variable i does not change.

Upvotes: 2

Related Questions