Hi again everyone. I am trying to change around this program I wrote to meet some new requirements, but getting stuck on how to do it.

*Write a program that reads a file consisting of students' test scores in the range of 0-200. The students scores should be stored into an array. It should then determine the number of students having sores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174, and 175-200.

The first line in the input file will contain the number of students in the class.*

This is what I have to do... the program I had original would read in all the scores in the text file and output them to the console just fine, but my original array was dealing with the ranges of scores. I now need to change that so the student scores are used for the array, and the first number in the file is to show how many students in the class. I was trying to just edit my old program thinking it would be simple but I cant seem to get it to compile or run after all sorts of different tweaks. Any help appreciated.

-thanks-

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

public class Grades
{
  public static void main(String[] args) 
                                   throws FileNotFoundException
  {
    Scanner inFile = new Scanner(new FileReader("scores.txt"));

    int range1, range2, range3, range4, range5, range6, range7, range8;
    int numstudents, sgrades;
    int begin = 0;  
    int end;
    
    int[] gradeArray = new int[sgrades];
    
    for (int i = 0; i < gradeArray.length; i++)
      gradeArray[i] = 0;

    while (inFile.hasNext())
    {
      int score = inFile.nextInt(); 
      int index = score / 25;        
      gradeArray[index]++;           
    }       
    
    for (int i = 0; i < gradeArray.length; i++)
    {
      if (i == gradeArray.length - 1)
      {
        end = begin + 25; 
      }
      else
      {
        end = begin + 24;
      }
      
      System.out.println(begin + "-" + end + ": " + gradeArray[i]);
      begin = end + 1;  
    }

    inFile.close();
  }
}

Ok, few problems that I can see:

  • In line 16 you are creating an array int[] gradeArray = new int[sgrades]; when you didn't initialize sgrades - you need to retrieve the number of students in the class, which is the first line in the file.
  • In the loop in line 21 you are ignoring the fact that the first line is different than the rest of the lines, and treat it as a regular line that holds a student's score.

You have to read the first line, determine the number of students, initialize the array, and only then retrieve the scores.
Also - please explain what you are trying to do in line 28 and beyond.

ok hmm so firstly should it be like

int[] gradeArray = new int[numstudents]; line 16

int numstudents = inFile.nextInt(); for loop on 21
int score = inFile.nextInt();
does that seem right??

also for lines 28 on that was what I had before to set the range of like 0-24,25-49 etc and have it print out how many of each score fit in each range.

Again, how will you create int[] gradeArray = new int[numstudents]; on line 16 if you initialize numstudents only on line 21?

so then int numstudents = inFile.nextInt(); should be before int[] gradeArray = new int[numstudents];

so line 15 lets say??

Yes. You always need to initialize a variable before using it, do you understand why?

ya that makes sense I dunno why I just wasn't seeing that, obviously you have to give it some value before it knows how to use it.

on a good note I got the program to compile and run and gives the right amount for each score range... however I am not entirely sure why but it prints out ranges up to 375...

wait never mind I figured it shortly after posting.. I just made another array to deal with the 8ranges! seems to work fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.