1

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; 
}
1
  • It would work better if you used a for loop to populate the numbers array because you know how many iterations there will be. Commented Apr 13, 2015 at 22:58

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1
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; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.