Im working on a problem today and I getting errors for some reason.
Problem statement:
Design and implement an application that creates a histogram, which allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1-10, 11-20, and so on. Print one asterisk for each value entered.
Here's the catch, making that problem is okay for me, but what if i wanted it to create it from reading values from a txt file?
This is my final code:
import java.util.*; import java.io.*; public class Histogram { public void displayIntArray(int[] iAr) { for(int index = 0; index < iAr.length; index++) { System.out.println(iAr[index]); } } public int[] readIntArrayFromFile(String fileName) throws Exception { Scanner fileScan = new Scanner(new File(fileName)); int counter = 0; while(fileScan.hasNext()) { fileScan.next(); counter++; } int[] iArray = new int[counter]; Scanner fScan = new Scanner(new File(fileName)); counter = 0; while(fScan.hasNext()) { iArray[counter] = fScan.nextInt(); counter++; } return iArray; } public int[] initializeCounterArray(int[] inArray) { final int SIZE = 10; final int MIN = 1; final int MAX = 100; int[] counters = new int[SIZE]; int n, nextt, ix; while (n < 0); { int[] numbers = new int[n]; } for (int index = 0; index < inArray.length; index++) { int number = inArray[index]; if (number >= MIN && number <= MAX) { int newIndex = (number - 1) / SIZE; counters[newIndex]++; } } return counters; // Creates the histogram values String[] stars = {" 1 - 10 |", "11 - 20 | ", "21 - 30 | ", "31 - 40 | ", "41 - 50 | ", "51 - 60 | ", "61 - 70 | ", "71 - 80 | ", "81 - 90 | ", "91 - 100 | "}; //10 strings for the different parameters for (ix = 0; ix < n; ix++) { nextt = numbers[ix]; if (nextt < 11) { stars[0] += "*"; } else { if (nextt < 21) { stars[1] += "*"; } else { if (nextt < 31) { stars[2] += "*"; } else { if (nextt < 41) { stars[3] += "*"; } else { if (nextt < 51) { stars[4] += "*"; } else { if (nextt < 61) { stars[5] += "*"; } else { if (nextt < 71) { stars[6] += "*"; } else { if (nextt < 81) { stars[7] += "*"; } else { if (nextt < 91) { stars[8] += "*"; } else { stars[9] += "*"; } } for (ix = 0; ix < 10; ix++) { System.out.println(stars[ix]); } } } } } } } } } } }
I've got the driver class all good to go but just in case:
public class Driver { public static void main(String[] args) throws Exception { int[] numbers, counters; Histogram hist = new Histogram(); numbers = hist.readIntArrayFromFile("data-8.3.txt"); counters = hist.initializeCounterArray(numbers); hist.displayIntArray(counters); } }
The only error im getting from this is the " nextt = numbers[ix]; " part of the code. ive tried implementing some commands at the top but nothing worked. But does everything look good? So if you feel that the code is not good, please feel free to make any required changes.
I appreciate everyone taking their time out of their day to help me out, it really means alot
Thank you!