Wrox Programmer Forums
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 9th, 2006, 01:53 AM
Registered User
 
Join Date: Sep 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Array help!

Hi everyone,

I've been struggling with setting up the arrays in the following problem:

The problem says: Write a program that reads a file consisting of students' test scores in the range 0-200. It should then determine the number of students having scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174, and 175-200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.)

I can set up the program but not the array...

Code:
import java.io.*;
import java.util.*;

public class Scores {

    static final int MAX_SCORE = 200;
    static final int CATEGORY_DIFFERENCE = 25;

    public static void main(String[] args) throws FileNotFoundException {

        int[] categories = new int[MAX_SCORE/CATEGORY_DIFFERENCE];

        Scanner inFile = new Scanner(new FileReader("a:\\testscores.txt"));

        while(inFile.hasNext()) {
            int score = inFile.nextInt();

          categories[(score==MAX_SCORE)?categories.length-1:score/CATEGORY_DIFFERENCE]++;

        }

    inFile.close();

    }
}
 
Old November 22nd, 2006, 08:16 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Works here. I added a bit to have it print its results:

Code:
// ...
inFile.close();
for (int i=0;i<categories.length;i++) {
    System.out.println("Category " + (i+1) + " had " + categories[i] + " students")  
}
// ...
Prints:

Code:
charlie@charlie:~$ java Scores
Category 1 had 1 students
Category 2 had 2 students
Category 3 had 0 students
Category 4 had 6 students
Category 5 had 1 students
Category 6 had 3 students
Category 7 had 5 students
Category 8 had 8 students
charlie@charlie:~$ java -version
java version "1.5.0_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_08-b03)
Java HotSpot(TM) Client VM (build 1.5.0_08-b03, mixed mode, sharing)
charlie@charlie:~$ uname -a
Linux charlie 2.6.17-2-k7 #1 SMP Wed Sep 13 17:18:46 UTC 2006 i686 GNU/Linux
charlie@charlie:~$
HTH

--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convering a String Array to an Integer array nkrust C# 9 November 17th, 2010 12:02 PM
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
error when sorting an Array of Array nancy VBScript 2 February 17th, 2005 12:57 PM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM
Array to Array comparison pavel Pro VB 6 0 March 24th, 2004 06:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.