I am trying to assign variables in my array diveScore using a for-each loop. However, I don't understand why it isn't working and every element is 0.0 after the loop has ended. Eclipse is showing no errors so I am sure there is something wrong with my logic.

Thanks.

double[] diveScores = new double[7];
		int scoreCount = 1;
		for (double element : diveScores)
		{
			System.out.println("Enter score " + scoreCount);
			
			diveScores[scoreCount-1] = keyboard.nextDouble();
			
			if (element < 0 || element > 10)
			{
				System.out.println("Score out of range 0 - 10. Please enter a valid score.");
				element = keyboard.nextDouble();
			}
			
			scoreCount++;
		}

The for loop body shows that after assignments are made to every elements of the array diveScore the guard starts to check the "input variable element". (It's too late). Actually no assignment has been made to the variable element. If only the values varying from 0 to 10 are acceptable in terms of valid scores, the guard you have set up ("if ...") can not do the checking job. You should use "while loop" as the guard, and place the guard before any assignment is made to the array of diveScore. I have modified your code as follows so that only the scores between 0 and 10 pass the guard and then assigned to the array of diveScore.

import java.io.*;
import java.util.*;

public class KeyBoard {

public static void main(String args[]) {
Scanner keyboard=new Scanner(System.in);
double[] diveScores = new double[7];
		int scoreCount = 1;
		for (double element : diveScores)
		{
			System.out.println("Enter score " + scoreCount);
			
			element = keyboard.nextDouble();
			
			while (element < 0 || element > 10)
			{
				System.out.println("Score out of range 0 - 10. Please enter a valid score.");
				element = keyboard.nextDouble();
			}
			
			diveScores[scoreCount-1] = element;			
			scoreCount++;
		}
		for (double d : diveScores)
		System.out.print(d + " ");
		System.out.println();
	}
}

Edited 6 Years Ago by tong1: n/a

First of all, I couldn't see any reason why you wouldn't get values assigned, so I ran this (the only changes I made were to make keyboard an instance of Scanner and to add an iteration at the end to print out the values). It assigned seven values for seven variables. It did not check them for range, but the assignment happened.

Tong's covered the range checking pretty well. A do while would be even better, something of the form:

do
{ get input and put it in the right place}
while input out of range;

but that's nitpicking.

What you do need to know is that your code is unnecessarily complicated by your use of the enhanced for. EnFor is great for reading, but not for writing, here's why (From the JLS, 14.14.2 if you want to read along at home):

Otherwise, the Expression necessarily has an array type, T[]. Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement. Then the meaning of the enhanced for statement is given by the following basic for statement:

T[] a = Expression;
L1: L2: ... Lm:
for (int i = 0; i < a.length; i++) {
        VariableModifiersopt Type Identifier = a[i];
        Statement
}

end quote.

So what's happening is that element takes the value of the i[I]th[/I] place in the array, but you're working on a copy. You actually seem to have got this, mostly. It's just in the range-checking part that you lost it and tried to assign into element.

So, if you're modifying the array or the Iterable, use a for (0..array.length-1) or a while (Iterable.hasNext()) instead of the EnFor.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.