Results 1 to 2 of 2
  1. #1
    space4rent00 is offline Member
    Join Date
    Feb 2010
    Rep Power

    Default 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.util.Scanner;
    public class Sales
    	public static void main(String[] args) throws Exception
    		Scanner input = new Scanner(new File("sale"));
    		int[] array = new int[7];
    		int[] frequency = new int[11];
    		double[] gross = new double[7];
    		double total;
    		int x = 0;
    		for(int i=0; i<array.length; i++)
    		System.out.printf("Salary Range\tNumber\n");
    		for (int range=2; range<frequency.length; range++)
    				System.out.printf("  $%d+:", 1000);
    				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
    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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    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 = /*???*/
            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.

Similar Threads

  1. printing number of pos/neg numbers input?
    By shroomiin in forum New To Java
    Replies: 12
    Last Post: 09-25-2009, 03:15 AM
  2. Log4j not printing out file and line number
    By devin in forum Java Servlet
    Replies: 2
    Last Post: 03-05-2009, 04:16 PM
  3. Replies: 1
    Last Post: 11-03-2008, 03:38 PM
  4. Replies: 2
    Last Post: 11-11-2007, 09:07 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:13 AM

Posting Permissions

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