By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,792 Members | 1,306 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,792 IT Pros & Developers. It's quick & easy.

Histogram Java, need help completing it

P: 1
Okay, so I have the program complete but i cannot figure out how to get the second part.

It asks, "Print an asterisk for every five values in each category." My program right now prints an asterisk for every value.

This is my program so far:

Expand|Select|Wrap|Line Numbers
  1. //*****************************************************************
  2. //Histogrm1.Java
  3. //
  4. //Java program that takes numerical inputs between 1-100 and prints
  5. //them in to categories of 10 with a * to represent each 5 number
  6. //values
  7. //*****************************************************************
  8.  
  9. import java.io.*;
  10. public class Histogram1 {
  11.  
  12.    // Main method
  13.    public static void main(java.lang.String[] args) throws IOException {
  14.  
  15.       //Storage of int 
  16.       DataInputStream stdin = new DataInputStream (System.in);
  17.       final int MAXRANGE = 10;       
  18.       final int MINRANGE = 1;         
  19.       final int RANGE    = 10;       
  20.  
  21.       int[] list = new int[MAXRANGE]; 
  22.  
  23.       for (int i=0; i<list.length; i++) {
  24.          list[i] = 0;
  25.       }
  26.  
  27.       // Enter Integers of range
  28.       System.out.println ("Enter some numbers between 1 and 100.");
  29.       System.out.println ("Signal the end by entering a number outside of that range.");
  30.  
  31.       // Entering the actual number
  32.       System.out.print ("Enter Integer: ");
  33.       int value = Integer.parseInt (stdin.readLine());
  34.  
  35.       // Will keep adding integers within range
  36.       while (value >= MINRANGE && value <= (MAXRANGE*RANGE)) {
  37.  
  38.          // Range
  39.          list[(value-1)/RANGE] = list[(value-1)/RANGE] + 1;
  40.  
  41.          // Enter next integer
  42.          System.out.print ("Enter Integer: ");
  43.          value = Integer.parseInt (stdin.readLine());
  44.       }
  45.  
  46.       // Print histogram
  47.       System.out.println ("\nThis is the histogram:");
  48.       for (int i=0; i<list.length; i++) {
  49.  
  50.          System.out.print ("   " + (i*RANGE+1) + " - " + (i+1)*RANGE + "\t| ");
  51.  
  52.          // Print *
  53.          for (int j=0 ; j<list[i] ; j++) {
  54.             System.out.print ("*");
  55.          }
  56.          System.out.println ();  
  57.       }
  58.    }
  59. }
Oct 4 '12 #1
Share this Question
Share on Google+
2 Replies


zmbd
Expert Mod 5K+
P: 5,397
This reads very much like a homework question...
Benefit of the doubt?
Look at looping commands.
Oct 4 '12 #2

zmbd
Expert Mod 5K+
P: 5,397
As there has been no reply, I'm closing this thread on grounds that homework problems are not allowed.
Oct 5 '12 #3

This discussion thread is closed

Replies have been disabled for this discussion.