I desperately need help with an assignment. I have been working on it for an hour and just can't figure it out. I'm sure it's far easier for some of you to figure out.
So the assignment says: Generate a table similar to the one illustrated below by reading the ages of members of the family who are planning to attend the reunion. The first data item in the file represents the number of ages that need to be read from the file since not all members are able to attend.
The output should look like this:
Age Group Distribution Percentage
<20 3 17.65
20-39 4 23.53
40-59 5 29.41
60-79 3 17.65
>79 2 11.76
This is the code I have so far.. I don't think I'm doing it correctly. Any feedback would be awesome!
import java.util.Scanner;
import java.io.*;
class Stats
{
public static void main(String args[])
{
try
{
FileReader data = new FileReader("data.txt");
BufferedReader numBuffer = new BufferedReader(data);
Scanner numScan = new Scanner(numBuffer);
int dist = 0;
double perc = 0;
System.out.println("Langner Family");
System.out.println("Age Group Distribution Percentage ");
while(numScan.hasNextInt())
{
int n = numScan.nextInt();
if(n<20)
{
dist++;
}
}
System.out.println("<20"+"\t"+"\t"+dist);
while(numScan.hasNextInt())
{
int n = numScan.nextInt();
if(n<39)
{
dist++;
}
}
System.out.println("20-39"+"\t"+"\t"+dist);
numScan.close();
}
catch (Exception e)
{
System.err.println("Error");
}
}
}

New Topic/Question
Reply



MultiQuote




|