Re: Help with an Array Assignment



sanchezj wrote:
import java.io.*;
import not required
import java util.*;
missing dot

missing class header
public static void main (String [ ] args) throws FileNotFoundException
Nothing throws that exception.
{
int [ ] scores = new int [50];
Is that the right number to cope with 0 to 50??
Scanner inFile = new Scanner(new FileReader (f:\\scores.txt));;
filename is a String - use quotes. You don't need the FileReader. Extra ;. Is whitespace the correct delimiter?

readData(inFile, scores); print(scores); System.out.println(); }

public static void readData(Scanner inputFile, int[] list)
    {
		int score;
		int index;

       	while (inputFile.hasNext())
	   	{
            score = inputFile.nextInt();
Think about your task - score & index are the same thing.
            index = score /  ???;

		  	if (index == list.length)
			  	index--;
		  	if (index < list.length)
				list[index]++;
Why do you think you need those "if's"?
 	}
 	}

    public static void print(int[] list)
    {
		int range;
	 	int lowRange = 0;
	  	int upperRange = 50;
There is no range involved - you are totalling the students with each possible score.

System.out.println(" Range # of Students");

	 	for (range = 0; range < list.length; range++)
	   	{
		  	System.out.printf("%4d - %3d %10d%n", lowRange,
                              upperRange, list[range]);
            lowRange = upperRange + 1;
			upperRange = upperRange + 25;
		   	if (range == list.length - 2)
			   	upperRange++;
		}
	  	System.out.println();
    }
}

Basically, you need to think about the logic of what you are trying to achieve - then try coding it.


Mark
.