Re: Help with an Array Assignment
- From: anon <anon>
- Date: Mon, 28 Nov 2005 12:21:57 +0000
sanchezj wrote:
import java.io.*;import not required
import java util.*;missing dot
missing class header
public static void main (String [ ] args) throws FileNotFoundExceptionNothing 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?
Think about your task - score & index are the same thing.
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();
Why do you think you need those "if's"?index = score / ???;
if (index == list.length) index--; if (index < list.length) list[index]++;
There is no range involved - you are totalling the students with each possible score.} }
public static void print(int[] list) { int range; int lowRange = 0; int upperRange = 50;
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 .
- References:
- Help with an Array Assignment
- From: sanchezj
- Help with an Array Assignment
- Prev by Date: Re: cant's compile with javac
- Next by Date: Newbie question on stopping an application
- Previous by thread: Re: Help with an Array Assignment
- Next by thread: (help plz) help me in downloading weblogic latest version
- Index(es):