Hi, I'm a freshman and I just started programing. I have an assignment due soon, and it's almost finished. My only problem is there is one build error that tells me ".class expected". This is on the same line as a for statement... and that really confuses me. Here's my code. Bear with me, I know it might make some of you cringe . :)

/**
 * @(#)LetterGrade.java
 *
 * LetterGrade application
 *
 * @author: JanieRae 
 * @version 1.00 2009/11/23
 */
import java.util.Scanner; 
public class findMedian {
    
    public static void main(String[] args) {
     Scanner keyboard = new Scanner (System.in);
    
    int total;
    int median;
    int[] nums;
    nums = new int[12];
    
    System.out.println(" Enter the first value");
         nums [0] = keyboard.nextInt();
    System.out.println(" Enter the second value"); 
         nums [1] = keyboard.nextInt();
    System.out.println(" Enter the third value"); 
         nums [2] = keyboard.nextInt();
    System.out.println(" Enter the fourth value"); 
         nums [3] = keyboard.nextInt();
    System.out.println(" Enter the fifth value"); 
         nums [4] = keyboard.nextInt();
    System.out.println(" Enter the sixth value"); 
         nums [5] = keyboard.nextInt();
    System.out.println(" Enter the seventh value"); 
         nums [6] = keyboard.nextInt();
    System.out.println(" Enter the eighth value"); 
         nums [7] = keyboard.nextInt();
    System.out.println(" Enter the ninth value"); 
         nums [8] = keyboard.nextInt();
    System.out.println(" Enter the tenth value"); 
         nums [9] = keyboard.nextInt();
    System.out.println(" Enter the eleventh  value"); 
         nums [10] = keyboard.nextInt();
     
   
   for ();
        {
            total = nums[0] + nums[1] + nums[2] + nums[3] + nums[4] + nums[5] +nums[6] + nums[7] + nums[8]+ nums[9]+ nums[10]+ nums[11];
            median = total / 11; 
        }
    
    }
}

ooops, i didn't type what i put in the for loop. I guess that's because i don't really know what to put in it.

I imagine your assignment was to use for-loops for the array. Ok, so basically an array is a list of items (in this case integers) and each is assigned a certain index. You seem to understand the basic usages of an array, as your syntax in that respect is correct. However, you are missing the main point -- why arrays are used. Why don't we just use variables A-Z to hold 26 values. Why do we use nums[0]-nums[25]? The answer lies in the for loop.

Because arrays use numbered indexes, we can perform the same operation on all the indexes by looping through the array as follows:

int[] nums = new int[12];
int indexCounter;
for (indexCounter=0;indexCounter<12;indexCounter++)
{
    //prints "enter value 1" for the first iteration, then "enter value 2", etc, etc, until the final "enter value 12"
    System.out.print("Enter value "+(indexCounter+1));

    //stores the first inputted value in nums[0], then nums[1], etc. until nums[11] is filled.
    nums [indexCounter] = keyboard.nextInt();
}

Do you see how this for-loop enables you to shorten your code drastically? Use this to get the numbers, and use a similar technique to find the sum of all the array's values to find the average.

Note: you should know that a median is not an average. A mean is an average. A median is the middle number of the set. For instance, in the set 1 1 7 9 12, the average is 6, but the median is 7.

Here is a for loop that can iterate over an array

for (int i = 0; i < array.length; i++){
    //Now add your code in here to access your array etc
}

Yeah, but as the OP said . .

Hi, I'm a freshman and I just started programing.

The simpler loop helps you understand indexing arrays. The fancier one does not.

Yeah, but as the OP said . .

Hi, I'm a freshman and I just started programing.

The simpler loop helps you understand indexing arrays. The fancier one does not.

Doesn't matter. Most times you use an array you never need the index, making the new syntax far simpler.

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