# Thread: Printing the Number of Times a Number in a Range Shows up

1. Member Join Date
Feb 2010
Posts
3
Rep Power
0

## Printing the Number of Times a Number in a Range Shows up

I'm reading 7 sales figures from a file (5000, 7000, 3000, 50, 100500, 75000, 300) and putting them into an array. I then calculate how much they should be paid (200 plus 9% of what they sold) and store that in a new array. My problem is getting the output to show how many salaries fall into a certain range.
Java Code:
```import java.io.File;
import java.util.Scanner;

public class Sales
{
public static void main(String[] args) throws Exception
{
Scanner input = new Scanner(new File("sale"));

int[] array = new int;
int[] frequency = new int;
double[] gross = new double;
double total;
int x = 0;

while(input.hasNext())
{
array[x++]=input.nextInt();
}
input.close();

for(int i=0; i<array.length; i++)
{
total=array[i]*.09+200;
gross[i]=total;
}

System.out.printf("Salary Range\tNumber\n");

for (int range=2; range<frequency.length; range++)
{
if(range==10)
System.out.printf("  \$%d+:", 1000);
else
System.out.printf("\$%3d-%3d:\n", range*100,range*100+99);
}
}
}```
Is what I have so far and the output I get is
Java Code:
```Salary Range	Number
\$200-299:
\$300-399:
\$400-499:
\$500-599:
\$600-699:
\$700-799:
\$800-899:
\$900-999:
\$1000+:```
However as I said I need a column under number to show how many of the 7 salaries will fall in that range. (The seven salaries being 650, 830, 470, 204.5, 9245, 695, 227) I know I need something in my second for loop following the if else to output the number of times it shows up but I can't figure it out. Thanks for any help.  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## I know I need something in my second for loop following the if else to output the number of times it shows up

Actually you need two things: you have to calculate the number of elements in the gross array that are in the appropriate range, and then you have to do the output.

And what you have to do at the in the second for loop to print that number of times (it's called the "frequency" and the different ranges are called "bins") is really easy: you just use printf()! You mention doing this after the if/else, but in fact if you calculate the frequency before you print a line you can print that line with a single printf().

Java Code:
```for (int range=2; range<frequency.length; range++)
{
int freq = /*???*/
if(range==10)
{
System.out.printf("  \$%d+:\t%d", 1000, freq);
} else {
System.out.printf("\$%3d-%3d:\t%d\n", range*100,range*100+99, freq);
}
}```

All of which raises the question: how do you find freq. The answer is that you use a for loop and check every element of the gross array. If that element lies within a bin (as determined by the current value of range) you increment the frequency counter.

-----

Your method is getting too long. (which is what lies behind the fact that you can see you have to do something at a certain point, but aren't sure what...) Give serious thought to breaking it up into separate methods. Candidates would be:

(a) A method to create an array read the contents of the file into it and return it.
(b) A method to take the input array and create and return another one populated with the gross pays.
(c) A method that takes the gross pay array and returns and array of frequencies.
(d) A method that takes an array of frequencies and prints it
Last edited by pbrockway2; 02-05-2010 at 11:45 PM.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•