0

I need to populate a double array with user input by using nested while loops only. This is what I have so far:

public static double[][] score() {
        int col = 3;
        int row = 3;
        int size = 0;
        Scanner in = new Scanner(System.in);
        double[][] scores = new double[row][col];
        System.out.println("Enter your scores: ");
        while (in.hasNextDouble() && size < scores.length) {
            while (size < scores[size].length) {
                scores[][] = in.hasNextDouble();
                size++;
            }
            return scores;
        }
2
  • Do you know the size of the array before hand? Commented Nov 17, 2017 at 23:49
  • 3 rows, 3 columns so i need 9 total inputs. Commented Nov 17, 2017 at 23:53

1 Answer 1

4

The most common way to do this is through for loops since they allow you to specify the index counters you need in a concise way:

for(int i = 0; i < scores.length; i++){
    for(int j = 0; j < scores[i].length; j++){
        scores[i][j] = in.nextDouble();
    }
}

If you specifically need to use while loops you can do pretty much the same thing, its just broken up into multiple lines:

int i = 0;
while(i < scores.length){
    int j = 0;
    while(j < scores[i].length){
        scores[i][j] = in.nextDouble();
        j++;
    }
    i++;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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