5 Replies - 382 Views - Last Post: 29 November 2012 - 04:38 PM Rate Topic: -----

#1 ThatBeast  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 16-November 12

Java External File Statistics

Posted 29 November 2012 - 12:42 PM

Hey Everyone,

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");
    }
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Java External File Statistics

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java External File Statistics

Posted 29 November 2012 - 01:26 PM

show us what data.txt looks like
Was This Post Helpful? 0
  • +
  • -

#3 ThatBeast  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 16-November 12

Re: Java External File Statistics

Posted 29 November 2012 - 01:27 PM

View Postpbl, on 29 November 2012 - 01:26 PM, said:

show us what data.txt looks like


data.txt is just a list of numbers..

17
80
79
60
21
20
1
39
40
59
61
4
16
47
52
35
54
85
21
15
48
49
50
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java External File Statistics

Posted 29 November 2012 - 01:30 PM

You have two while in a row with

while(numScan.hasNextInt())

sure that when one is done the other won't have any more num to read. So that logic is false

first number is 17 but then 22 numbers follow
How do you exaplain

Quote

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.

What is 17 representing ?
Was This Post Helpful? 0
  • +
  • -

#5 ThatBeast  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 16-November 12

Re: Java External File Statistics

Posted 29 November 2012 - 01:51 PM

View Postpbl, on 29 November 2012 - 01:30 PM, said:

You have two while in a row with

while(numScan.hasNextInt())

sure that when one is done the other won't have any more num to read. So that logic is false

first number is 17 but then 22 numbers follow
How do you exaplain

Quote

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.

What is 17 representing ?

17 is the number of items that have to be read..
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java External File Statistics

Posted 29 November 2012 - 04:38 PM

But there are 22 in the file ?
So use an array for all groups as 20 per group an array of 5 should do
Anyhow if you say so...


    int[] count = new int[5];
    int nb = numScan.nextInt();
    for(int i = 0; i < nb; ++i) {
       int val = numSCan.nextyInt();
       count[value/20]++;
    }

    for(int i = 0; i < count.length; ++i) {
       int from = i * 20;
       System.out.println("From " + from + " to " + (from+19) + ": " + count[i]);
    }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1