Note that there are some explanatory texts on larger screens.

plurals
  1. POBeginning Java (Histogram)
    primarykey
    data
    text
    <p>I'm taking a beginner Java class, with the assignment to create a histogram program with the following output: (100 and 10 are user inputs).</p> <p>How many numbers? 100 How many intervals? 10</p> <pre><code>Histogram -------------------------------------------------------- 1 ****(4) 2 ******(6) 3 ***********(11) 4 *****************(17) 5 **************************(26) 6 *************************(25) 7 *******(7) 8 ***(3) 9 (0) 10 *(1) -------------------------------------------------------- </code></pre> <p>My code is giving the following output however, can anyone help me point out what is going wrong, thanks so much.</p> <pre><code>How Many Numbers? 10 How Many Intervals? 10 Histogram -------------------------------------------------------- 1 **********(10) 2 **********(10) 3 **********(10) 4 **********(10) 5 **********(10) 6 **********(10) 7 **********(10) 8 **********(10) 9 **********(10) 10 **********(10) </code></pre> <p>For the input, 100 and 10 I get the error message:</p> <blockquote> <p>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at Lab6.main(Lab6.java:44) I marked Line 44 below. </p> </blockquote> <p>Which links to this code;</p> <pre><code> intervalValue[j]++; </code></pre> <p>I'm not sure how to attach the generator (.jar) file, it is supposed to just generate the random #'s for us. Thanks again.</p> <pre><code>mport java.util.Scanner; public class Lab6 { public static void main(String[] args) { int numbers, intervals; double intervalWidth; double max, mins, range; Scanner keyboard = new Scanner(System.in); System.out.print("How Many Numbers? "); numbers = keyboard.nextInt(); System.out.print("How Many Intervals? "); intervals = keyboard.nextInt(); double [] generate = new double[numbers]; generate = randomGenerator(numbers); max = maximum(generate); mins = minimum(generate); range = max - mins; intervalWidth = range / intervals; int [] intervalValue = new int[intervals]; for (int i=0; i &lt; generate.length; i++) { for (int j = 0; j&lt;generate.length; j++){ double imin = mins+j*intervalWidth; double imax = max +j*(intervalWidth); if(generate[i] &gt;= imin &amp;&amp; generate[i] &lt; imax) intervalValue[j]++; //LINE 44 } } System.out.println("Histogram"); System.out.println("--------------------------------" + "------------------------"); for (int a=0; a &lt; intervalValue.length; a++) { System.out.print(" " + (a+1) + " "); for (int b=0; b &lt; intervalValue[a]; b++) { System.out.print("*"); } System.out.println("(" + intervalValue[a] + ")"); } } private static double [] randomGenerator(int number) { double [] generate; generate = Generator.getData(number); return generate; } private static double maximum(double [] a) { double max = a[0]; for (int i = 1; i &lt; a.length; i++) { if (a[i] &gt; max) { max = a[i]; } } return max; } private static double minimum(double [] a) { double mins = a[0]; for (int i = 1; i &lt; a.length; i++) { if (a[i] &lt; mins) { mins = a[i]; } } return mins; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload