mur7ay
mur7ay

Reputation: 833

Creating a histogram within Java

I've created a histogram with the following code:

import java.util.*; 

public class Murray_A03Q3 {
    public static void main (String [] args){

        int num = 1;
        int[] nums = new int[10];  

        List<Integer> list = new ArrayList<Integer>();

        Scanner scan = new Scanner(System.in);


        while(num != 0){
           System.out.print("Enter a value to plot: ");
           num = scan.nextInt();
           System.out.println();
           if(num != 0)
              list.add(num);
        }


        for (int a = 0; a < list.size(); a++){
            nums[(list.get(a)-1) / 10]++;
        }

        for (int count = 0; count < 10; count++){
           System.out.print((count*1+1) + (count == 1 ? " ": " ") + "| \t");

           for(int h = 0; h < nums[count]; h++)
              System.out.print("#");

           System.out.println();
        }

    } // end of main

} // end of class Murray

With the output being:

1 |     ######
2 |     
3 |     
4 |     
5 |     
6 |     #
7 |     
8 |     
9 |     #
10 | 

But I need it to print the values from the users input from 1-10 not 1-100 like it seems to be printing (the output above used two values outside of ten which is why one was printed at 6 & 9). I've scanned through the code and tried to manipulate it different ways to actually get what I wanted, but I can't figure it out. My question is, what actually needs to be changed to get the values between 1-10?

Thanks for your help!

Upvotes: 2

Views: 6517

Answers (2)

MC10
MC10

Reputation: 572

You should limit it so they can only input 1-10. In your nums array, I just stored the number of occurences of that index number. Then when printing, just print the index+1 and the inner loop does the #'s for you.

import java.util.*;

public class Murray_A03Q3{
   public static void main (String [] args){

      int num = 1;
      int[] nums = new int[10];  

      List<Integer> list = new ArrayList<Integer>();

      Scanner scan = new Scanner(System.in);

      while(num != 0){
         System.out.print("Enter a value to plot: ");
         num = scan.nextInt();
         System.out.println();
         if(num > 0 && num <= 10)
            list.add(num);
         if(num > 10 || num < 0)
                System.out.println("Please enter a number 1-10");
      }

      for (int a = 0; a < list.size(); a++){
         nums[a] = Collections.frequency(list, a);
      }

      for (int count = 0; count < 10; count++){
         System.out.print((count+1) + "\t|");

         for(int h = 0; h < nums[count]; h++)
            System.out.print("#");

         System.out.println();
      }
   } // end of main
} // end of class Murray

I also moved the line in front of the tab to make it look more even. This was input for 1, 2, 3. It used to display all 3 #'s on the 1 line.

1   |#
2   |#
3   |#
4   |#
5   |
6   |
7   |
8   |
9   |
10  |

Upvotes: 1

David M
David M

Reputation: 841

Well your loop is wrong when you're adding to nums if you want to exclude things outside 10. Change

for (int a = 0; a < list.size(); a++){
    nums[(list.get(a)-1) / 10]++;
}

to

for (int a = 0; a < list.size(); a++) {
    if (list.get(a)-1 < 10) {
        nums[list.get(a)]++;
    }
}

Upvotes: 0

Related Questions